有一个网址:
/foo
我希望所有内容都像这样重定向:
/foo/xxx --> /bar/xxx
/foo/xxx/yyy --> /bar/xxx/yyy
Django url-pattern 是如何将所有 foo 重定向到 bar 的?
类似这样的吗?
url(r'^foo/????$', RedirectView.as_view())
这就是我用于“零级”重定向的方法,它有效:
url(r'^foo/?$', RedirectView.as_view(pattern_name='template_name'))
最佳答案
捕获 url 模式中路径的其余部分。
并将与/bar/
组合的路径传递给redirect
function :
url(r'^foo/(.*)', lambda request, path: redirect('/bar/' + path))
关于python - 将用户重定向到新 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26123416/