Jurudocs
Jurudocs

Reputation: 9165

Regex django url

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'),
)

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

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

Upvotes: 15

Views: 19006

Answers (2)

Nicolas Cortot
Nicolas Cortot

Reputation: 6701

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'),
)

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.

Upvotes: 33

Deelaka
Deelaka

Reputation: 13693

Try this regex instead:

\/mobile-thing\/(?P<uuid>.*)\/$

So it'd be:

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

Upvotes: 2

Related Questions