Page not found 404 Django media files Page not found 404 Django media files python python

Page not found 404 Django media files


Add media url entry in your project urlpatterns:

from django.conf.urls.static import staticfrom django.conf import settings...urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


The better way for MEDIA_ROOT is,

try to make media path dynamic will be easy when you shift your project.

Settings.py

BASE_DIR = os.path.dirname(os.path.dirname(__file__))MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')MEDIA_URL = '/media/'

urls.py

from django.conf import settingsfrom django.conf.urls.static import staticurlpatterns = [    # ... the rest of your URLconf goes here ...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Look at this

https://docs.djangoproject.com/en/dev/howto/static-files/


Just to add: in case the other answers do not work for you, try putting the static url before the other ones. Like so:

urlpatterns = static(...) + [...]

What may be happening is that some of your patterns in the list prevent the request from reaching the static handlers. So putting the static handlers first solves this. Worked for me.