我有一个Django网站。我想将模式为servertest
的URL重定向到相同的URL,除了servertest
应该被server-test
替换。
因此,例如,以下URL将被映射为重定向,如下所示:
http://acme.com/servertest/ => http://acme.com/server-test/
http://acme.com/servertest/www.example.com => http://acme.com/server-test/www.example.com
http://acme.com/servertest/www.example.com:8833 => http://acme.com/server-test/www.example.com:8833
我可以使用url.py中的以下行获得第一个工作示例:
('^servertest/$', 'redirect_to', {'url': '/server-test/'}),
不知道如何为其他人做,所以只替换URL的servetest部分。
试试这个表达式:
或者这个:
('^server test','重定向到',{'url':'/server test/'})
It's covered in the docs.
(我非常强调。)
然后他们的例子:
所以你看这只是一个简单明了的形式:
使用以下(为Django 2.2更新):
它在
servertest/
之后接受零个或多个字符,并将它们放在/server-test/
之后。或者,您可以使用新的
path
函数,该函数不使用regex就可以覆盖简单的案例url模式(在Django的新版本中,它是首选):相关问题 更多 >
编程相关推荐