如何在 django urlpatterns 中转义特殊字符

发布于 2024-12-02 13:16:01 字数 625 浏览 0 评论 0原文

所以,假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼睛会笑 2024-12-09 13:16:01

根据 re 模块 的 Python 手册:

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

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

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

According to the Python manual for the re module:

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.

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),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文