如何在Django URL模式中取消特殊字符的转义 [英] how to unescape special characters in django urlpatterns

查看:137
本文介绍了如何在Django URL模式中取消特殊字符的转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设我有一个django应用,需要响应如下网址: http://127.0.0.1:8000/food%20log/4/up/.原始参数是食物日志",但是当包含在url中时,需要用%20替换空格.现在,该链接已被单击,并且返回到urls.py.

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)/$', process_vote),

...)

因此,似乎它不能正确识别参数.这是在要使用具有空格的content_type之前的功能代码.如果我们暂时无法在整个系统的其余部分中仅从该content_type的名称中删除空格,那么如何获取urlpatterns函数以识别"food%20log"实际上是"food log",以便将其识别为有效?

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 ?

基本上,我想在urlpatterns对其进行处理之前对字符串进行预处理,但是我不确定如何/在何处进行处理.感谢您的协助.

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.

推荐答案

根据 re模块:

未指定LOCALE和UNICODE标志时,匹配任何字母数字字符和下划线;这等效于集合[a-zA-Z0-9_].使用LOCALE,它将与集合[0-9_]以及当前语言环境定义为字母数字的任何字符相匹配.如果设置了UNICODE,则它将匹配字符[0-9_]加上Unicode字符属性数据库中分类为字母数字的任何字符.

When the LOCALE and UNICODE flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9_]. With LOCALE, it will match the set [0-9_] plus whatever characters are defined as alphanumeric for the current locale. If UNICODE is set, this will match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties database.

因此,除了\ w外,还可能在正则表达式中包含空格.

So maybe include the space in the regular expression in addition to \w.

urlpatterns = patterns('',
(r'^(?P<content_type>[\w ]+)/(?P<object_id>\d+)/(?P<direction>up|down|clear)/$',     process_vote),

这篇关于如何在Django URL模式中取消特殊字符的转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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