views:

264

answers:

2

Hi, I want march a django-URL with just 2 alternatives /module/in/ or /module/out/

Actually Im using

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

But matches with other patterns like /module/i/, /module/n/, /module/ou/; etc.

Any hint is apreciated :)

A: 

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

bstpierre
you suggest like this?: ^(?P<status>\w+(in|out))/$'
panchicore
Yes, that's correct.
bstpierre
request to: /status/in/ django says: The current URL, /status/in/, didn't match any of these. ^(?P<status>\w+(in|out))/$'
panchicore
Ok, I guess I need more context. Do you have a higher-level urls.py? If so, is it stripping the /status/ off the front? If not, you probably want '^(?P<status>\w+/(in|out))/$'
bstpierre
+2  A: 

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 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.

Alasdair
Yes, now matchs exactly with in|out. thx.
panchicore