为我的Django项目中的每个应用创建自定义404错误? [英] Create custom 404 error for each app on my django project?

查看:71
本文介绍了为我的Django项目中的每个应用创建自定义404错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为Django项目中的每个应用程序创建多个自定义错误模板,我的意思是,在我的项目中,我有3个应用程序,每个应用程序将显示3种不同的custom 404错误.

is there a way to create multiple custom errors templates for each app on my Django project, I mean, in my project I got 3 apps I will show 3 different customs 404 error per each app.

现在,我正在为我的后台办公室应用程序和前台办公室显示相同的404错误页面.

Right now I'm showing the same 404 error page for my back office app and front office.

推荐答案

创建自定义错误视图,并将其分配给根 urls.py :

Create a custom error view and assign it to handler404 variable in your root urls.py:

from django.views.defaults import page_not_found

def my_error_404(request, exception):
    template_name = '404.html'
    if request.path.startswith('/backoffice/'):
        template_name='backoffice/404.html'
    elif request.path.startswith('/frontoffice/'):
        template_name='frontoffice/404.html'
    return page_not_found(request, exception, template_name=template_name)

此代码适用于Django 1.9.如果使用django< = 1.9,则从视图中删除 exception 参数.

This code is for django 1.9. If you use django <= 1.9 then remove the exception parameter from the view.

这篇关于为我的Django项目中的每个应用创建自定义404错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆