正则表达式Django URL [英] Regex django url

查看:101
本文介绍了正则表达式Django URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个网址,我想匹配uuid
,网址看起来像这样:

Hello I have a url and I want to match the uuid the url looks like this:


/ mobile / mobile-thing / 68f8ffbb-b715-46fb-90f8-b474d9c57134 /



urlpatterns = patterns("mobile.views",
    url(r'^$', 'something_cool', name='cool'),
    url(r'^mobile-thing/(?P<uuid>[.*/])$', 'mobile_thing', name='mobile-thinger'),
)

但这根本不起作用。我的对应观点没有被调用。我测试了很多变化... ahw

but this doesn't work at all. My corresponding view is not being called. I tested so many variations...ahw

但是 url(r'^ mobile-thing /',' mobile_thing',name ='mobile-thinger')就像一个咒语,但没有任何组合...

but url(r'^mobile-thing/', 'mobile_thing', name='mobile-thinger') works like a charm but no group...

推荐答案

[。* /] 表达式仅匹配一个字符,可以是 * / 。您需要改写(这只是许多选项之一):

The [.*/] expression only matches one character, which can be ., * or /. You need to write instead (this is just one of many options):

urlpatterns = patterns("mobile.views",
    url(r'^$', 'something_cool', name='cool'),
    url(r'^mobile-thing/(?P<uuid>[^/]+)/$', 'mobile_thing', name='mobile-thinger'),
)

这里, [^ /] 表示除 / + 之后一次或多次匹配此类字符。您不希望最后的 / 放在 uuid var中,因此请将其放在括号之外。

Here, [^/] represents any character but /, and the + right after matches this class of character one or more times. You do not want the final / to be in the uuid var, so put it outside the parentheses.

这篇关于正则表达式Django URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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