如何为所有其他在django中找不到的url编写模式? [英] How to write pattern for all other urls that basically goes for not found in django?

查看:75
本文介绍了如何为所有其他在django中找不到的url编写模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过这个,


url('',views.notfound,name ='notfound')

url ('', views.notfound, name='notfound')

但似乎无法正常工作,例如,我定义了另一个网址模式,

But seems it doesn't work properly, for example, I have another url pattern define,


url(r'^ login / $',views.login,name ='login'),

url(r'^login/$', views.login, name='login'),

所以,如果我去 http://example.com/login/ ,这行得通,但是如果我去 http:// example.com/login/?help=1 ,则它属于未找到类别。我该如何处理?

So, if I go for http://example.com/login/, this works, but if i go for http://example.com/login/?help=1, then it falls into notfound category. How can I handle that?

推荐答案

如果您只想创建一个找不到URL时显示的页面(即抛出Http404异常),则可以创建一个名称为 404.html 的模板,并且该模板将在找不到URL时显示。

If you just want to create a page that is displayed when a URL is not found (i.e. a Http404 exception is thrown) you can create a template with the name 404.html, and that template will be displayed any time a URL is not found.

或者,如果要定义自定义404处理程序视图,则可以在中定义 handler404 = views.notfound urls.py 文件。然后,只需创建您的 notfound 视图,并且只要抛出404错误(并且 DEBUG = False

Or if you want to define a custom 404 handler view you can define handler404 = views.notfound in your urls.py file. Then just create your notfound view, and that view will be used whenever a 404 error is thrown (and DEBUG = False)

这是捕获任何无法识别的URL并显示友好的404页面的更好方法。

This is a better way to catch any urls that are not recognized and display a friendly 404 page.

这篇关于如何为所有其他在django中找不到的url编写模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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