Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django catch-all URL without breaking APPEND_SLASH

Tags:

django

I have an entry in my urls.py that acts as a catch-all which loads a simple view if it finds an appropriate page in the database. The problem with this approach is that the URL solver will then never fail, meaning that the APPEND_SLASH functionality won't kick in - which I need.

I'd rather not have to resort to adding a prefix to the static page URLs to stop it being a catch-all. I do know about flatpages, which uses a 404 hook rather than an entry in urls.py, and I had kinda hoped to avoid having to use it, but I guess this problem might be exactly the kind of reason why one would use it.

Any way round this problem or should I just give in and use flatpages?

like image 237
nedned Avatar asked Jul 01 '11 08:07

nedned


1 Answers

Make sure that your catch-all URL pattern has a slash at the end, and that the pattern is the last in your URLconf. If the catch-all pattern doesn't end with a slash, then it will match stray URLs before the middleware tries appending a slash.

For example, use r'^.*/$' instead of r'^.*' as your last pattern.

To do the same, but pass the url to the view as a named argument, use r'^(?P<url>.*)/$'.

like image 62
Arthur Hebert-Ryan Avatar answered Sep 23 '22 22:09

Arthur Hebert-Ryan