Django在丢失的模板上显示404

时间:2022-08-29 20:22:36

I have a website where some pages are edited by hand. When one of those templates is missing, it just means that the page is not present, so I would like to display an Error 404.

我有一个网站,有些页面是手工编辑的。当其中一个模板丢失时,这仅仅意味着页面不存在,所以我想显示一个错误404。

Instead I get an exception TemplateDoesNotExist.

相反,我得到一个异常模板不存在。

Is there a way to tell Django to display an error 404 whenever it does not find a template?

是否有一种方法可以告诉Django在它没有找到模板时显示一个错误404 ?

3 个解决方案

#1


12  

If you want this behaviour for all views on your site, you might want to write your own middleware with a process_exception method.

如果您希望对站点上的所有视图执行这种行为,您可能希望使用process_exception方法编写自己的中间件。

from django.template import TemplateDoesNotExist
from django.views.defaults import page_not_found

class TemplateDoesNotExistMiddleware(object):
    """ 
    If this is enabled, the middleware will catch
    TemplateDoesNotExist exceptions, and return a 404
    response.
    """

    def process_exception(self, request, exception):
        if isinstance(exception, TemplateDoesNotExist):
            return page_not_found(request)

If you have defined your own handler404 you would need to replace page_not_found above. I'm not immediately sure how you could convert the string handler404 into the callable required in the middleware..

如果您已经定义了自己的handler404,那么需要替换上面的page_not_found。我不能立即确定如何将字符串handler404转换为中间件中所需的可调用。

To enable your middleware, add it to MIDDLEWARE_CLASSES in settings.py. Be careful of the position where you add it. The standard Django middleware warning applies:

要启用中间件,请将其添加到settings.py中的MIDDLEWARE_CLASSES。注意你加的位置。使用标准的Django中间件警告:

Again, middleware are run in reverse order during the response phase, which includes process_exception. If an exception middleware returns a response, the middleware classes above that middleware will not be called at all.

同样,中间件在响应阶段以相反的顺序运行,其中包括process_exception。如果异常中间件返回响应,则根本不会调用该中间件之上的中间件类。

#2


9  

put the return of the response in the view (or whereever the template is rendered) in a try-except block:

将响应的返回值放在视图中(或模板被呈现的地方),并使用try-except代码块:

from django.http import Http404
from django.shortcuts import render_to_response
from django.template import TemplateDoesNotExist

def the_view(request):
    ...
    try:
        return render_to_response(...)
    except TemplateDoesNotExist:
        raise Http404

#3


-1  

Off the top of my head, but if you set DEBUG=False in your settings, won't you get a 404 then on every error (including TemplateNotFound)?

我想不起来了,但是如果在设置中设置DEBUG=False,那么每个错误(包括TemplateNotFound)都不会得到404吗?

#1


12  

If you want this behaviour for all views on your site, you might want to write your own middleware with a process_exception method.

如果您希望对站点上的所有视图执行这种行为,您可能希望使用process_exception方法编写自己的中间件。

from django.template import TemplateDoesNotExist
from django.views.defaults import page_not_found

class TemplateDoesNotExistMiddleware(object):
    """ 
    If this is enabled, the middleware will catch
    TemplateDoesNotExist exceptions, and return a 404
    response.
    """

    def process_exception(self, request, exception):
        if isinstance(exception, TemplateDoesNotExist):
            return page_not_found(request)

If you have defined your own handler404 you would need to replace page_not_found above. I'm not immediately sure how you could convert the string handler404 into the callable required in the middleware..

如果您已经定义了自己的handler404,那么需要替换上面的page_not_found。我不能立即确定如何将字符串handler404转换为中间件中所需的可调用。

To enable your middleware, add it to MIDDLEWARE_CLASSES in settings.py. Be careful of the position where you add it. The standard Django middleware warning applies:

要启用中间件,请将其添加到settings.py中的MIDDLEWARE_CLASSES。注意你加的位置。使用标准的Django中间件警告:

Again, middleware are run in reverse order during the response phase, which includes process_exception. If an exception middleware returns a response, the middleware classes above that middleware will not be called at all.

同样,中间件在响应阶段以相反的顺序运行,其中包括process_exception。如果异常中间件返回响应,则根本不会调用该中间件之上的中间件类。

#2


9  

put the return of the response in the view (or whereever the template is rendered) in a try-except block:

将响应的返回值放在视图中(或模板被呈现的地方),并使用try-except代码块:

from django.http import Http404
from django.shortcuts import render_to_response
from django.template import TemplateDoesNotExist

def the_view(request):
    ...
    try:
        return render_to_response(...)
    except TemplateDoesNotExist:
        raise Http404

#3


-1  

Off the top of my head, but if you set DEBUG=False in your settings, won't you get a 404 then on every error (including TemplateNotFound)?

我想不起来了,但是如果在设置中设置DEBUG=False,那么每个错误(包括TemplateNotFound)都不会得到404吗?