为我的 Django 项目中的每个应用程序创建自定义 404 错误?
Create custom 404 error for each app on my django project?
有没有办法为我的 Django 项目中的每个应用程序创建多个自定义错误模板,我的意思是,在我的项目中我有 3 个应用程序,我将为每个应用程序显示 3 个不同的自定义 404 错误。
现在我正在为我的后台应用程序和前台显示相同的 404 错误页面。
创建自定义 error view 并将其分配给根 urls.py
中的 handler404
变量:
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
参数。
有没有办法为我的 Django 项目中的每个应用程序创建多个自定义错误模板,我的意思是,在我的项目中我有 3 个应用程序,我将为每个应用程序显示 3 个不同的自定义 404 错误。
现在我正在为我的后台应用程序和前台显示相同的 404 错误页面。
创建自定义 error view 并将其分配给根 urls.py
中的 handler404
变量:
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
参数。