开发者

Django- trying to use APPEND_SLASH and a custom catchall 404 view

开发者 https://www.devze.com 2023-03-01 01:55 出处:网络
I generally add a catch-all 404 regex to my Django websites as the last expression in my urls.py: urlpatterns += patterns(\'django.views.generic.simple\',

I generally add a catch-all 404 regex to my Django websites as the last expression in my urls.py:

urlpatterns += patterns('django.views.generic.simple',
    (r'^.', 'direct_to_template', {'template': 'unknown.html'}),

I'm generally happy with the performance of this. The unknown.html template extends my base template and nicely tells the viewer that their entered url doesn't exist, but the page still has all the navigation and style of my website.

However, after having to repeatedly tell people to enter a trailing slash, I feel that the APPEND_SLASH = True parameter in settings.py needs to be set.

the docs state:

If APPEND_SLASH is True and the initial URL doesn’t end with a slash, and it is not found in the URLconf, then a new URL is formed by appending a slash at the end. If this new URL is found in the URLconf, then Django redirects the request to this new URL. Oth开发者_如何学Cerwise, the initial URL is processed as usual.

So following this logic, foo.com/bar is successfully caught by my "404" url expression before it can be redirect to my foo.com/bar/ url expression. *

What is the best way to maintain a friendly/custom catchall 404 page while also being able to use APPEND_SLASH or something with similar functionality?*

--edit/answer--

Somehow I missed that you just need to add a template named 404.html, and also make sure DEBUG = False

Thanks DTing!


I think you can just customize your 404.html instead of using a "catchall" since you are just redirecting to a custom template. There is no reason why your custom 404.html template can't extend your site's base.html.

http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views

Three things to note about 404 views:

  • The 404 view is also called if Django doesn't find a match after checking every regular expression in the URLconf.
  • If you don't define your own 404 view -- and simply use the default, which is recommended -- you still have one obligation: you must create a 404.html template in the root of your template directory. The default 404 view will use that template for all 404 errors. The default 404 view will pass one variable to the template: request_path, which is the URL that resulted in the 404.
  • The 404 view is passed a RequestContext and will have access to variables supplied by your TEMPLATE_CONTEXT_PROCESSORS setting (e.g., MEDIA_URL).
  • If DEBUG is set to True (in your settings module), then your 404 view will never be used, and the traceback will be displayed instead.

if you do want to use a custom view,

This page_not_found view should suffice for 99% of Web applications, but if you want to override the 404 view, you can specify handler404 in your URLconf, like so:

handler404 = 'mysite.views.my_custom_404_view'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号