当debug = True时,当debug = False时,什么可能导致Django错误

时间:2022-08-16 20:46:17

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

使用开发服务器,它使用debug = True或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".

在生产中,如果debug = True,一切正常,但如果debug = False,我得到500错误,apache日志以导入错误结束:“ImportError:无法导入名称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).

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

4 个解决方案

#1


7  

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.

如果您在其中一个文件中进行循环导入,则会发生这种情况。检查并查看是否从Project导入了某些内容,然后从最初导入Project的原始文件中导入Project中的内容。

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

我最近遇到了同样的问题,重新安排我的一些进口帮助解决了这个问题。

#2


76  

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

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

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

但是,请注意,您需要根据实际主机(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).

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

感谢gertvdijk指出这一点

#3


1  

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!

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

#4


1  

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

我也有这个问题。虽然它甚至在设置Allowed_hosts并且已经拥有404和500模板时仍然存在。

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

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

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

我终于让django生成了一个日志文件,https://*.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.

我不小心留下了一个“get_host”函数,该函数现在存在于使用Django 1.5的HttpRequest(更改为HttpRequest.get_host())下。

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

由于某些原因,没有使用Debug True或False引发错误。

#1


7  

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.

如果您在其中一个文件中进行循环导入,则会发生这种情况。检查并查看是否从Project导入了某些内容,然后从最初导入Project的原始文件中导入Project中的内容。

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

我最近遇到了同样的问题,重新安排我的一些进口帮助解决了这个问题。

#2


76  

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

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

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

但是,请注意,您需要根据实际主机(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).

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

感谢gertvdijk指出这一点

#3


1  

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!

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

#4


1  

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

我也有这个问题。虽然它甚至在设置Allowed_hosts并且已经拥有404和500模板时仍然存在。

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

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

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

我终于让django生成了一个日志文件,https://*.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.

我不小心留下了一个“get_host”函数,该函数现在存在于使用Django 1.5的HttpRequest(更改为HttpRequest.get_host())下。

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

由于某些原因,没有使用Debug True或False引发错误。