Home > Net >  How to fix Django Summernote using relative path for src url in image integration?
How to fix Django Summernote using relative path for src url in image integration?

Time:02-28

I am new to the Django framework, and inherited a projet which I am trying to fix. Right now my project runs in a docker at url localhost:8000 until deployment in on a remote server.

I have the django-summernote extension installed for fancy text editing and image integration, but it doesn't work as expected:

Summernote Editor in Django admin works fine, and correctly displays uploaded images

But on my content pages, the html integration of my provides an incorrect src url (which return an error ofc)

src="/media/django-summernote/2022-02-27/90a1f1ec-ed16-4cc2-a9f8-b0bb30ea4ab8.png"

The expected html integration should be :

src="http://localhost:8000/media/django-summernote/2022-02-27/90a1f1ec-ed16-4cc2-a9f8-b0bb30ea4ab8.png"

I checked and the image file does exist in the folder, the expected url below does actually shows the picture alright as well.

The rest of the website uses images integration just fine when not related to summernote.

I figure this has something to do with settings.py or even router (which tbh is still a bit complex in my head).

settings.py

MEDIA_URL = "/media/"

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

urls.py

urlpatterns = [
...
path("summernote/", include("django_summernote.urls")),
]

Edit: running Django 3.1

CodePudding user response:

For Django >= 1.7 Just add this into your urls.py

urlpatterns = [
...
path("summernote/", include("django_summernote.urls")),
]  static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

You no longer need if settings.DEBUG as Django will handle ensuring this is only used in Debug mode.

For Django <= 1.6

if settings.DEBUG:
urlpatterns  = patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings.MEDIA_ROOT}))

CodePudding user response:

Try this:

settings.py

MEDIA_ROOT = BASE_DIR / "media"

MEDIA_URL = "/media/"

urls.py

urlpatterns = [
...
path("summernote/", include("django_summernote.urls")),
]

urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • Related