Skip to content Skip to sidebar Skip to footer

Django Admin: Image Saved But Error Occured When Click

I am ashamed to ask a question of that sort but I still can not solve my problem. I normally uploaded image in my media directory and can see image link in my admin but if I click

Solution 1:

You need to add the media MEDIA_ROOT and MEDIA_URL in your urlpatterns

urlpatterns = [
   url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

check following links for more details

Accessing "Media" files in Django

Django classifies user files in two types

  1. Static files
  2. Media files

this link helps you understand the difference between them

Your issues deals with Media files.

In future development, you may need to serve static files, to serve them you will need to add STATIC_ROOT and STATIC_URL to the urlpatterns in a similar way that MEDIA_ROOT and MEDIA_URL are added

Solution 2:

Change your URL's patterns:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
   url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

for more info Django Docs

Solution 3:

First point: don't put uploaded medias in you static directory - static is for static files that are part of your project (css, js etc) -, you want to use MEDIA_ROOT and MEDIA_URL for this.

wrt/ serving the static and media contents, it depends on the environment. On your local dev environment you want to use the staticfile.views to serve both static and medias as mentionned in the other answers, but don't do this in production : on a production server, you want to use your front web server to serve static (and media) contents.

Post a Comment for "Django Admin: Image Saved But Error Occured When Click"