当 debug=False 时可能导致 Django 错误的原因是当 debug=True 时不存在的错误

发布于 2024-10-16 18:04:33 字数 237 浏览 2 评论 0原文

使用开发服务器,它可以与 debug=True 或 False 一起使用。

在生产中,如果 debug=True,一切正常,但如果 debug=False,我会收到 500 错误,并且 apache 日志以导入错误结束:“ImportError:无法导入名称项目”。

导入中没有任何内容对调试有任何条件 - 唯一的代码是开发服务器是否应该提供静态文件(在生产中,apache 应该处理这个 - 并且这是单独测试的并且工作正常)。

Using the development server, it works with debug=True or False.

In production, everything works if debug=True, but if debug=False, I get a 500 error and the apache logs end with an import error: "ImportError: cannot import name Project".

Nothing in the import does anything conditional on debug - the only code that does is whether the development server should serve static files or not (in production, apache should handle this - and this is tested separately and works fine).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤君无依 2024-10-23 18:04:33

只是说,我今天遇到了类似的错误,这是因为 Django 1.5 需要设置中的 ALLOWED_HOSTS 参数。
您只需放置此行即可使其工作;)

...
ALLOWED_HOSTS = '*'
...

但是,请注意您需要根据实际主机正确设置此参数(https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts)!

此列表中的值可以是完全限定名称(例如“www.example.com”),在这种情况下,它们将与请求的主机标头完全匹配(不区分大小写,不包括端口)。以句点开头的值可用作子域通配符:“.example.com”将匹配 example.com、www.example.com 以及 example.com 的任何其他子域。 '*' 值将匹配任何内容;在这种情况下,您有责任提供自己的 Host 标头验证(可能在中间件中;如果是这样,则该中间件必须首先列在 MIDDLEWARE_CLASSES 中)。

因此,基本上,一旦投入生产,您最好使用这种类型的配置:

...
ALLOWED_HOSTS = [
    '.yourdomain.com',
]
...

感谢 gertvdijk 指出了这一点

Just to say, I ran into a similar error today and it's because Django 1.5 requires the ALLOWED_HOSTS parameter in the settings.
You simply need to place this row to make it work ;)

...
ALLOWED_HOSTS = '*'
...

However, be aware that you need to set this parameter properly according to your actual host(s) (https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts)!

Values in this list can be fully qualified names (e.g. 'www.example.com'), in which case they will be matched against the request’s Host header exactly (case-insensitive, not including port). A value beginning with a period can be used as a subdomain wildcard: '.example.com' will match example.com, www.example.com, and any other subdomain of example.com. A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE_CLASSES).

So basically it's better for you to use this type of configuration once you're in production:

...
ALLOWED_HOSTS = [
    '.yourdomain.com',
]
...

thanks to gertvdijk for pointing this out

魔法少女 2024-10-23 18:04:33

如果您的某个文件中有循环导入,就会发生这种情况。检查并查看您是否从 Project 导入某些内容,然后从最初导入 Project 的原始文件将某些内容导入到 Project 中。

我最近遇到了同样的问题,重新安排一些导入有助于解决问题。

This happens if you have a circular import in one of your files. Check and see if you are importing something from Project and then importing something in Project from the original file that originally imported Project.

I ran into this same problem recently, and rearranging some of my imports helped fix the problem.

淡笑忘祈一世凡恋 2024-10-23 18:04:33

如果您没有同时存在 500.html 和 404.html 模板,也可能会发生这种情况。仅 500 还不够好,即使对于不会生成 404 的 URI 也是如此!

This can also happen if you do not have both a 500.html and 404.html template present. Just the 500 isn't good enough, even for URIs that won't produce a 404!

拍不死你 2024-10-23 18:04:33

我也有这个问题。尽管即使设置Allowed_hosts并且已经有404和500个模板,它仍然存在。

我还检查了循环导入,但事实并非如此。

我终于让 django 生成了一个日志文件, https://stackoverflow.com/a/15100463/1577916

我不小心留下了“get_host”函数现在存在于
Django 1.5 中的 HttpRequest(更改为 HttpRequest.get_host())。

由于某种原因,调试 True OR False 没有引发错误。

I had this problem as well. Although it persisted even when setting Allowed_hosts and already having 404 and 500 templates.

I also checked for circular imports, but that was not it.

I finally had django produce a log file, https://stackoverflow.com/a/15100463/1577916

I accidentally left in a "get_host" function which now exists under
HttpRequest (changed to HttpRequest.get_host())with Django 1.5.

for some reason that was not raising an error with Debug True OR False.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文