如何在 django urlpatterns 中转义特殊字符
所以,假设我有一个 django 应用程序需要响应这样的 url: http: //127.0.0.1:8000/food%20log/4/up/ 。原来的参数是“food log”,但在url中包含时需要将空格替换为%20。现在链接已被点击,并且返回到 urls.py。
urlpatterns = patterns('',
(r'^(?P<content_type>\w+)/(?P<object_id>\d+)/(?P<direction>up|down|clear)/$', process_vote),
... )
因此,看来它无法正确识别该参数。这是在想要使用其中有空格的 content_type 之前的功能代码。如果我们暂时假设我不能在系统的其余部分中删除该 content_type 名称中的空格,那么如何让 urlpatterns 函数识别“food%20log”实际上是“food log”,这样它就会将其识别为有效的?
基本上我想在 urlpatterns 作用之前对字符串进行预处理,但我不确定如何/在哪里执行此操作。感谢您的任何帮助。
So, suppose I have a django app which needs to respond to a url like this: http://127.0.0.1:8000/food%20log/4/up/ . The original parameter is "food log", but it needed to have the space replaced with %20 when it was included in the url. Now the link has been clicked on, and it's coming back to urls.py.
urlpatterns = patterns('',
(r'^(?P<content_type>\w+)/(?P<object_id>\d+)/(?P<direction>up|down|clear)/
...
)
So, it appears that it is not able to properly recognize the parameter . This is functional code prior to wanting to use a content_type which has a space in it. If we assume for the moment that I can't just remove the space from the name of that content_type throughout the rest of the system, how do I get the urlpatterns function to recognize that "food%20log" is actually "food log", so that it will recognize it as a valid ?
Basically I want to preprocess the string before it's acted on by urlpatterns, but I am not sure how/where to do that. Thanks for any assistance.
, process_vote),
...
)
So, it appears that it is not able to properly recognize the parameter . This is functional code prior to wanting to use a content_type which has a space in it. If we assume for the moment that I can't just remove the space from the name of that content_type throughout the rest of the system, how do I get the urlpatterns function to recognize that "food%20log" is actually "food log", so that it will recognize it as a valid ?
Basically I want to preprocess the string before it's acted on by urlpatterns, but I am not sure how/where to do that. Thanks for any assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 re 模块 的 Python 手册:
因此,除了 \w 之外,还可以在正则表达式中包含空格。
According to the Python manual for the re module:
So maybe include the space in the regular expression in addition to \w.