如果在Django的urls.py中找不到URL,则将其重定向到404.html [英] Redirect any urls to 404.html if not found in urls.py in django

查看:214
本文介绍了如果在Django的urls.py中找不到URL,则将其重定向到404.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果urls.py中不存在任何类型的url模式,而不是被django显示错误,如何将其重定向到已创建的页面 404.html页面。

How can I redirect any kind of url patterns to a created page "404.html" page if it doesn't exist in the urls.py rather than being shown the error by django.

推荐答案

创建一个视图,将呈现您创建的404.html,并将其设置为urls.py中的handler404。

Make a view that'll render your created 404.html and set it as handler404 in urls.py.

handler404 = 'app.views.404_view'

如果启用了调试,Django将呈现调试视图。否则,它将为处理程序404中指定的所有类型的页面(如果不存在)呈现404页面。

Django will render debug view if debug is enabled. Else it'll render 404 page as specified in handler404 for all types of pages if it doesn't exist.

自定义错误视图

检查答案以获取完整示例。

Check this answer for a complete example.

这篇关于如果在Django的urls.py中找不到URL,则将其重定向到404.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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