I am trying to make a django application using django-markdownx
and dj3-cloudinary-storage
together.
So one of the features of django-markdownx
is that you can drag and drop image in the markdown field and it saves that image and gives back the path of the image. And in local this works just fine. Drag and drop an image to the makrdown field and it saves it to the /media/markdownx/
path as expected and the image path in the markdown field is correct. However after connecting with Cloudinary this does not work correctly. After dragging and dropping an image it saves the image to Cloudinary. But the image path in the markdown field is not correct.
This is the path of an image when I drag and drop in the template ![](https://<domain>/<username>/image/upload/v1/media/markdownx/f44db8f1-f5b3-488b-b4f8-e8c730156746.jpg)
This is the path of an image when I drag and drop in the admin ![](https://<domain>/<username>/image/upload/v1/media/markdownx/b41a8009-399d-4cc3-950a-7394536eece9.jpg)
However this is the actual path in Cloudinary.
image saved from template https://<domain>/<username>/image/upload/v1595344310/media/markdownx/f44db8f1-f5b3-488b-b4f8-e8c730156746_nlek8c.jpg
image saved from admin https://<domain>/<username>/image/upload/v1595344381/media/markdownx/b41a8009-399d-4cc3-950a-7394536eece9_fgpoob.jpg
Now from the path I can see that the version(I assume) part is different and the last part is messing after _
.
But how can I fix this? Or is this just simply not possible to achieve?
Could not find a solution in the documents of both django-markdownx
and dj3-cloudinary-storage
packages so any advice/recommendations are very helpful as well. Basically if I can save images in markdown to cloudinary that will be a win.
Here are the necessary codes.
Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django = "*"
pillow = "*"
autopep8 = "*"
dj3-cloudinary-storage = "*"
django-markdownx = "*"
[requires]
python_version = "3.8"
settings.py(necessary parts)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.forms', # for django-markdownx
# third party
'cloudinary_storage',
'cloudinary',
'markdownx',
# local
'pages.apps.PagesConfig',
]
# media
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# cloudinary configs
CLOUDINARY_STORAGE = {
'CLOUD_NAME': <user_name>,
'API_KEY': <public_key>,
'API_SECRET': <secret_key>,
}
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('markdownx/', include('markdownx.urls')),
path('', include('pages.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In my pages
app these are the code.
models.py
from django.db import models
from django.urls import reverse
from markdownx.models import MarkdownxField
class Page(models.Model):
title = models.CharField(max_length=255)
description = MarkdownxField()
cover = models.ImageField(upload_to='covers/', blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("pages:detail", kwargs={"pk": self.pk})
views.py
from django.views.generic import CreateView, DetailView
from .models import Page
class PageDetailView(DetailView):
model = Page
template_name = 'detail.html'
class PageCreateView(CreateView):
model = Page
template_name = 'new.html'
fields = ('title', 'description', 'cover',)
urls.py
from django.urls import path
from .views import PageCreateView, PageDetailView
app_name = 'pages'
urlpatterns = [
path('new/', PageCreateView.as_view(), name='new'),
path('<int:pk>/', PageDetailView.as_view(), name='detail')
]
Thank you in advance :)
When using cloudinary you can upload the asset with random characters, as-is or with random suffix.
Since
dj3-cloudinary-storage
is not officially supported by cloudinary, I'm not sure how to do it with it. but if you are using cloudinary SDK you can do: