我尝试编写django重定向功能。我提供了网址,我想重定向到提供的网址。
在urls.py
:
urlpatterns = patterns('',
url(r'^redirect/(?P<name>.*)$', redirect),
# ...
)
当我使用标准链接(例如google.com)测试该功能时,它可以完美运行。
当我使用包含“?”的链接测试该功能时字符,只有“?”之前的部分被考虑在内。
示例:
"GET /redirect/http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing?id=53713291 HTTP/1.1" 302 0
name = http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing
?id=53713291
未被考虑在内....
我虽然.*
意味着所有善良的角色,但错了吗?
你知道会发生什么吗?以及如何将entiere url包含在它包含的角色中?
非常感谢你的帮助。
答案 0 :(得分:0)
您似乎无法理解URL的工作原理。 ?
之后的所有内容都被解析为当前视图的参数。如果您在request.GET
字典中打印数据,您会发现类似的内容:
{'id': 53713291}
解决这个问题的唯一方法是在参数中对您的URL进行urlencode并在重定向之前对其进行解码。
>>> import urllib
>>> urllib.quote_plus("http://www.polyvore.com/lords_liverpool_hey_jude_tee/thig?id=53713291")
'http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291'
# You should make your URL with this value, for example:
# /redirect/http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291
# And in your view, use unquote_plus before the redirection:
>>> urllib.unquote_plus('http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291')
'http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing?id=5313291'
有关Query String on Wikipedia的更多信息。
答案 1 :(得分:0)
你正在传递一个正则表达式,所以你需要转义特殊字符(即\?)
但是,如果您尝试传递查询字符串参数,则需要以不同方式处理:https://stackoverflow.com/a/3711911