Python Django media url not working after setting DEBUG = True

妖精的绣舞 提交于 2019-12-20 05:42:08

问题


As stated in topic, my Django site media url is returning 404 after trying to access it. Everything was working flawless until I wanted to end the development process and set

DEBUG = True

in settings.py to have the site finished once and for all. When I change DEBUG back to

DEBUG = False

it works fine once again. I have no idea what's the problem, any suggestions?


回答1:


This is by design: https://docs.djangoproject.com/en/1.7/howto/static-files/#serving-static-files-during-development

If you use django.contrib.staticfiles as explained above, runserver will do this automatically when DEBUG is set to True.

That being said, you can use the following workaround by modifying your urls.py:

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

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Note that this is highly inefficient and not encouraged for production use. You should normally configure your web server (apache, nginx, etc) to serve your static and media content.



来源:https://stackoverflow.com/questions/28478159/python-django-media-url-not-working-after-setting-debug-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!