我有一个基于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
我可以使用urls.py中的以下行来获取第一个示例:
('^servertest/$', 'redirect_to', {'url': '/server-test/'}),
不确定如何为其他人执行此操作,因此只会更换URL的servetest部分。
答案 0 :(得分:3)
给定的URL可能包含字典样式的字符串格式,它将根据URL中捕获的参数进行插值。因为关键字插值总是完成(即使没有传入参数),URL中的任何“%”字符都必须写为“%%”,以便Python将将它们转换为输出上的单个百分号。
(强调我的。)
然后是他们的例子:
此示例从中发出永久重定向(HTTP状态代码301) / foo /< id> / to / bar /< id> /:
from django.views.generic.simple import redirect_to urlpatterns = patterns('', ('^foo/(?P<id>\d+)/$', redirect_to, {'url': '/bar/%(id)s/'}), )
所以你看到它只是一个很好的直截了当的形式:
('^servertest/(?P<path>.*)$', 'redirect_to', {'url': '/server-test/%(path)s'}),
答案 1 :(得分:3)
使用以下内容:
('^servertest/(?P<path>.*)$', 'redirect_to', {'url': '/server-test/%(path)s'}),
在servertest /之后需要零个或多个字符,并将它们放在/ server-test /。
之后答案 2 :(得分:0)
试试这个表达式:
('^servertest/', 'redirect_to', {'url': '/server-test/'}),
或者这一个:
('^ servertest','redirect_to',{'url':'/ server-test /'}),