Search code examples
djangodjango-media

Django medias(pictures) suddenly not loading


I am developing a website with Django, I had a lot of pictures in my project, uploaded from the admin panel and saved in the Media folder which I created for these uploads separately, It was working fine and exact way I wanted in months, Suddenly they are just not loading, getting 404 for all of them, without any change in project, they are just not loading. My media path in Settings.py :

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

I have added this to the end of my urls.py of the app:

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and as i said, it was working fine for a long, suddenly this happened

edit: I just figured it out that this is happening when I am using redirect function in one of my views


Solution

  • I had the same issue and this helps me :

    Add this in your urls.py in the urlpatterns list :

    from django.views.static import serve
    from django.conf import settings
    from django.conf.urls import url
    urlpatterns = [
    url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
    # YOUR URLS ARE HERE
    ]
    

    Of course stay in Debug=True in settings.py.