python - Django: match one or more from a set of keyworded regular expressions in an urlconf -


i have django view takes 3 optional keyworded arguments. want handle regular expression matching possible urls view in 1 line. want structure urls nicely.

an example: possible parameters start int, serial string of length 13, , end int.

an url might like:

/main/s20130509/e20130510/abc1234567890 or /main/s20130509/e20130510/ or  /main/abc1234567890 

where e , s prefixed components end , start respectively, , abc1234567890 serial.

i want pull these end, start, serial values , pass them view values start=s20130509, etc...

right doing exhaustively listing permutations on separate lines, , seems there must better way.

i'm trying like:

url(r'^base_url/(?p<serial>[^/]{13}|(?p<end>e\d{8})|(?p<start>s\d{8})/*$', view_method), 

basically, logic of want clear me; want pull instances of of 3 matches , pass them keyworded params, can't find resource figure out regex syntax fit this.

any thoughts? i'm happy pretty whatever gets job done elegantly.

thanks time,

tim

what you're wanting is:

url(r'^base_url/(?p<serial>[^/]{13}/$', view_method), 

with addition of optional groups end , start kwargs, so:

# optional, non-capturing group surrounding named group each (so don't have capture slashes or "e" or "s" (?:e(?p<end>\d{8})/) 

then, allow 2 of those, in either order:

((?:s(?p<start>\d{8})/)|(?:e(?p<end>\d{8})/)){0,2} 

the result is:

url(r'^base_url/((?:s(?p<start>\d{8})/)|(?:e(?p<end>\d{8})/)){0,2}(?p<serial>[^/]{13})/$', view_method), 

disclaimer, wrote in box, it'll take me moment test , update answer (if it's wrong).

update:

indeed, worked :) matched following:

http://127.0.0.1:8080/base_url/e77777777/s88888888/1234567890123/ http://127.0.0.1:8080/base_url/s88888888/e77777777/1234567890123/ http://127.0.0.1:8080/base_url/s88888888/1234567890123/ http://127.0.0.1:8080/base_url/e77777777/1234567890123/ http://127.0.0.1:8080/base_url/1234567890123/ 

the kwargs looked (raised in exception get method of sub-class of view when requested 3 segments - end and/or start none when left out):

{'start': u'88888888', 'serial': u'1234567890123', 'end': u'77777777'} 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -