panchicore
panchicore

Reputation: 11922

match an alternative url - regular expression django urls

I want a Django URL with just 2 alternatives /module/in/ or /module/out/

I'm using

url(r'^(?P<status>\w+[in|out])/$', 
'by_status', 
name='module_by-status'),

But it matches other patterns like /module/i/, /module/n/ and /module/ou/.

Any hint is appreciated :)

Upvotes: 3

Views: 5246

Answers (2)

Alasdair
Alasdair

Reputation: 308819

Try r'^(?P<status>in|out)/$'

You need to remove \w+, which matches one or more alphanumeric characters or underscores. The regular expression suggested in bstpierre's answer, '^(?P<status>\w+(in|out))/$' will match helloin, good_byeout and so on.

Note that if you use the vertical bar (pipe) character | in your url patterns, Django cannot reverse the regular expression. If you need to use the url tag in your templates, you would need to write two url patterns, one for in and one for out.

Upvotes: 10

bstpierre
bstpierre

Reputation: 31206

You want (in|out), the [] you are using indicate a character class containing the characters 'i', 'n', '|', 'o', 'u', 't'.

Upvotes: 1

Related Questions