什么会在 debug=False 时导致 Django 错误,而当 debug=True 时不存在

问题描述:

使用开发服务器,它可以使用 debug=True 或 False.

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

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

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".

导入中的任何内容都不会以调试为条件 - 唯一的代码是开发服务器是否应提供静态文件(在生产中,apache 应处理此问题 - 这已单独测试并且工作正常).>

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).

如果您在其中一个文件中有循环导入,就会发生这种情况.检查并查看您是否从 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.