Python Django media url 设置 DEBUG = True 后不工作
Python Django media url not working after setting DEBUG = True
如主题所述,我的 Django 站点媒体 url 在尝试访问它后返回 404。在我想结束开发过程并设置
之前,一切都完美无缺
DEBUG = True
在 settings.py 中一劳永逸地完成网站。当我将 DEBUG 改回
DEBUG = False
再次正常工作。我不知道有什么问题,有什么建议吗?
这是设计使然:https://docs.djangoproject.com/en/dev/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.
也就是说,您可以通过修改 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)
请注意,这是非常低效的,不鼓励用于生产。您通常应该配置您的网络服务器(apache、nginx 等)来提供您的静态和媒体内容。
如主题所述,我的 Django 站点媒体 url 在尝试访问它后返回 404。在我想结束开发过程并设置
之前,一切都完美无缺DEBUG = True
在 settings.py 中一劳永逸地完成网站。当我将 DEBUG 改回
DEBUG = False
再次正常工作。我不知道有什么问题,有什么建议吗?
这是设计使然:https://docs.djangoproject.com/en/dev/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.
也就是说,您可以通过修改 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)
请注意,这是非常低效的,不鼓励用于生产。您通常应该配置您的网络服务器(apache、nginx 等)来提供您的静态和媒体内容。