1

I currently have URLs of the form /blue - each URL is a colour. The associated URL pattern is as follows:

 (r'^(?P<colour>\w+)$', 'views.colour')

I'm wondering if it's possible to have URLs that look like a natural language list, of indeterminate length, separated by -or-:

/blue-or-green-or-yellow

Ideally the associated URL pattern would append each match to a Python list, ready to be handled in the view:

 (r'^(?P<colour_list>\w+)(?:-or-(?P<colour_list>\w+))+$', 'views.colour')

Is there any way to do this in Django?

3 Answers 3

4

Something like (?P<colour_list>(\w+(\-or\-)?)+) will get the entire substring match, then you can just split by -or-

Note, however, that then blue-or- would be valid match, so you may want to split it like this: filter(bool, colour_list.split('-or-'))

Sign up to request clarification or add additional context in comments.

Comments

0

Try this regex:

(\w+(?:-or-)?)+

or use string split:

result = colours.split("-or-")

1 Comment

That re.findall solution will only give the last color - try it with red-or-blue-green and you'll get ["green"]
0

Something like this will help:

takes comma separated colours

(r'^(?P<colours>[\w,]+)$', 'views.colour')

then in view:

colours = colours.split(',')

3 Comments

I think you were aiming for something more along the lines of ^(?P<colours>[\w,]+)$, note the position of the +
@DanielB yeah + should be there for multiple occurrences, just missed it
That + is still inside the square brackets, meaning red+blue,green would be a valid match

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.