设置DEBUG = True后,Python Django媒体URL不起作用 [英] Python Django media url not working after setting DEBUG = True

查看:181
本文介绍了设置DEBUG = True后,Python Django媒体URL不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如主题所述,我的Django网站媒体网址在尝试访问后返回404.在我想结束开发过程并进行设置之前,一切工作都完美无瑕

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

中的

可以使网站一劳永逸.当我将DEBUG改回

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?

推荐答案

这是设计使然:

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

如果您如上所述使用django.contrib.staticfiles,则当DEBUG设置为True时,runserver将自动执行此操作.

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

话虽如此,您可以通过修改urls.py使用以下变通方法:

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)

请注意,这种方法效率极低,不建议用于生产用途.通常,您应该配置Web服务器(apache,nginx等)以提供静态内容和媒体内容.

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.

这篇关于设置DEBUG = True后,Python Django媒体URL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆