Django捕捉所有网址,而不会打破APPEND_SLASH [英] Django catch-all URL without breaking APPEND_SLASH
问题描述
我宁愿不必须诉诸于向静态页面网址添加前缀,以阻止它成为全部的。我知道平板电脑,它使用404钩子而不是urls.py中的条目,我有希望避免使用它,但我猜这个问题可能正是这样一个原因。
任何方式解决这个问题,还是应该放入并使用平板?
确保您的全部URL模式在结尾处有斜杠,并且该模式是URLconf中的最后一个。如果catch-all模式不以斜线结尾,那么它将匹配流水网址,然后中间件尝试附加斜杠。
例如,使用 r'^。* / $'
而不是 r'^。*'
作为您的最后一个模式。
要做同样的事情,但是将URL作为命名参数传递给视图,请使用 r'^(?P< url>。*)/ $' code>。
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?
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>.*)/$'
.
这篇关于Django捕捉所有网址,而不会打破APPEND_SLASH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!