当 DEBUG 为 False 时,Django 平面页面引发 404(存在 404 和 500 模板)
我正在使用 Django 1.1.1 稳定版。当 DEBUG
设置为 True
Django 平面 工作正常;当 DEBUG 为 False 时,我尝试访问的每个平面页面都会引发自定义 404 错误(我的错误模板显然工作正常)。
在互联网上搜索建议创建 404 和 500 模板,我已经这样做了。
我已将 FlatpageFallBackMiddleware
添加到 middleware_classes,并将 flatpages 添加到已安装的应用程序中。有什么想法可以让平面工作吗?
I'm using Django 1.1.1 stable. When DEBUG
is set to True
Django flatpages works correctly; when DEBUG
is False
every flatpage I try to access raises a custom 404 error (my error template is obviously working correctly).
Searching around on the internet suggests creating 404 and 500 templates which I have done.
I've added to FlatpageFallBackMiddleware
to middleware_classes and flatpages is added to installed applications. Any ideas how I can make flatpages work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
同样的情况也发生在我身上,直到我发现 404 视图正在发送 200 状态响应。因此,您所要做的就是将其添加到处理 404 响应的视图中:
The same happened to me until I found that the 404 view was sending a 200 status response. So all you have to do is add this in the view that handles your 404 response:
尝试在
django.middleware.common.CommonMiddleware
之前添加FlatpageFallBackMiddleware
并确保您的 404.html 和 500.html 存储在模板目录的根目录中
(例如:templates/404.html)
try to add
FlatpageFallBackMiddleware
beforedjango.middleware.common.CommonMiddleware
and be sure, that your 404.html and 500.html are stored in the root of your templates dir
(eg: templates/404.html)
关键是检查中间件的顺序。中间件在in(请求和查看)方式上按自上而下的顺序执行,在out(响应和异常)方式上按自下而上的顺序执行。因此,如果您要在应该是完全合理的 Flatpage URL 上访问 404 处理程序,那么在调用 Flatpage 中间件之前,某些东西会捕获 404。
The key is checking the order of your middleware. Middleware is executed in top-down order on the way in (request and view) and in bottom-out order on the way out (response and exception). So if you are getting to your 404 handler on what should be a perfectly reasonable flatpage URL then something is catching the 404 before the flatpages middleware is getting called.
在不同的上下文中出现相同的错误。该问题是由于我按照
pylint
的建议将文件urls.py
从 更改为
引起的,但这省略了 handler404 和 handler500,预计它们将通过 import
隐式导入*。
因此,要么添加这些导入,要么按照 django 文档建议的那样导入
*
解决了问题。Had the same error in a different context. The problem was caused by me changing file
urls.py
fromto
as suggested by
pylint
, but this omits handler404 and handler500 which are expected to be imported implicitly by import*
.so either adding those to import or just importing
*
as django documents suggest solved the issue.创建一个错误处理视图,打印堆栈跟踪
import traceback;traceback.print_exc()
,而不是默默地忽略错误。Make an error handling view that prints a stacktrace
import traceback;traceback.print_exc()
instead of ignoring the error silently.