如何在Django中重写这个URL? [英] How do I rewrite this URL in Django?

查看:112
本文介绍了如何在Django中重写这个URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Django的网站。我想将URL中的模式servertest重定向到同一个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的最大部分被替换。

解决方案

使用以下内容:

 ('^ servertest /(?P< path>。*)$','redirect_to',{'url':'/ server-test / (路径)s'}),

servertest后需要零个或多个字符,并将它们放在/ server-test /。


I have a Django based website. I would like to redirect URLs with the pattern servertest in them to the same URL except servertest should be replaced by server-test.

So for example the following URLs would be mapped be redirected as shown below:

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 

I can get the first example working using the following line in urls.py:

('^servertest/$', 'redirect_to', {'url': '/server-test/'}),

Not sure how to do it for the others so only the servetest part of the URL is replaced.

解决方案

Use the following:

('^servertest/(?P<path>.*)$', 'redirect_to', {'url': '/server-test/%(path)s'}),

It takes zero or more characters after servertest/ and places them after /server-test/ .

这篇关于如何在Django中重写这个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆