Django找不到具有Debug = False和Allowed_Hosts的静态文件 [英] Django can't find staticfiles with Debug=False and Allowed_Hosts

查看:1740
本文介绍了Django找不到具有Debug = False和Allowed_Hosts的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解决这个问题时遇到麻烦:如果我将DEBUG设为False,我无法运行manage.py runserver:

  CommandError:如果DEBUG为False,则必须设置settings.ALLOWED_HOSTS 

我添加一些东西给ALLOWED_HOSTS:

  ALLOWED_HOSTS = ['*'] 

ALLOWED_HOSTS = [' localhost']

ALLOWED_HOSTS = ['127.0.0.1','localhost']

现在,我可以做'manage.py runserver',但是静态文件不起作用。奇怪的。



如果我将DEBUG设置为True,那么它的功能与ALLOWED_HOSTS设置为无关,localhost或*。所以,我猜这个问题与DEBUG有关。我不明白。

解决方案

DEBUG 模式中, Django开发服务器为您处理服务静态文件。然而,这不是最适合于生产,因为它比真正的服务器效率要低得多。请参阅此处


提供文件



除了这些配置步骤,您还需要实际提供静态文件。



在开发过程中,如果使用django.contrib.staticfiles,当DEBUG设置为True时,这将由runserver自动完成(请参阅django.contrib.staticfiles.views.serve())。 p>

此方法严重效率低下,可能不安全,因此不适合生产。



请参阅部署静态文件在生产环境中提供静态文件的策略。


查看此处了解如何在生产中提供静态文件。



编辑:将以下内容添加到回答@alejoss关于使用DEBUG = True查看错误页面的问题。 p>

我添加了以下内容到我的根目录 urls.py 文件:

  if settings.DEBUG:
urlpatterns + = patterns(
'',
url(r'^ 400 / $',TemplateView .as_view(template_name ='400.html')),
url(r'^ 403 / $',TemplateView.as_view(template_name ='403.html')),
url(r' 404 / $','django.views.defaults.page_not_found'),
url(r'^ 500 / $','django.views.defaults.server_error'),

您可能需要更改一点(即 400 如果您的模板名称不同,则可能需要编辑 403 页面。基本上,您可以访问 http:// localhost / 400 以查看您的 400 错误页, http:// localhost / 403 查看您的 403 错误页面,依此类推。


Hi all I'm having trouble solving this issue: If I turn DEBUG to False, I can't run manage.py runserver:

CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False

Then, let's say I add something to ALLOWED_HOSTS:

ALLOWED_HOSTS = ['*']
or
ALLOWED_HOSTS = ['localhost']
or
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

Now, I can do ´manage.py runserver´ but the staticfiles don't work. Weird.

If I turn DEBUG to True, then it works with ALLOWED_HOSTS set to nothing, to localhost or to *. So, I guess the problem has to do with DEBUG. I don't understand it.

解决方案

In DEBUG mode, the Django development server handles serving static files for you. However, this is not best for production as it's much more inefficient than a true server. See here.

Serving the files

In addition to these configuration steps, you’ll also need to actually serve the static files.

During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).

This method is grossly inefficient and probably insecure, so it is unsuitable for production.

See Deploying static files for proper strategies to serve static files in production environments.

Check out here to learn out how to serve static files in production.

EDIT: Adding the following to answer @alejoss question about viewing error pages with DEBUG=True.

I added something like the following to my root urls.py file:

if settings.DEBUG:
    urlpatterns += patterns(
        '',
        url(r'^400/$', TemplateView.as_view(template_name='400.html')),
        url(r'^403/$', TemplateView.as_view(template_name='403.html')),
        url(r'^404/$', 'django.views.defaults.page_not_found'),
        url(r'^500/$', 'django.views.defaults.server_error'),
    )

You might need to alter a bit (i.e., the 400 and 403 pages may need to be edited if your template names are different). Basically, this lets you visit http://localhost/400 to see your 400 error page, http://localhost/403 to see your 403 error page, and so on.

这篇关于Django找不到具有Debug = False和Allowed_Hosts的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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