当 debug=False 时可能导致 Django 错误的原因是当 debug=True 时不存在的错误
使用开发服务器,它可以与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是说,我今天遇到了类似的错误,这是因为 Django 1.5 需要设置中的 ALLOWED_HOSTS 参数。
您只需放置此行即可使其工作;)
但是,请注意您需要根据实际主机正确设置此参数(https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts)!
因此,基本上,一旦投入生产,您最好使用这种类型的配置:
感谢 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 ;)
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)!
So basically it's better for you to use this type of configuration once you're in production:
thanks to gertvdijk for pointing this out
如果您的某个文件中有循环导入,就会发生这种情况。检查并查看您是否从 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.
如果您没有同时存在 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!
我也有这个问题。尽管即使设置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.