[Answer]-Django URLs: matching a fixed number of optional parameters?

1👍

I’ve done a similar thing, I solved it like this:

url.py:

url(r'^(?P<location>[\w]+)/(?P<month>[\w]*)/?(?P<something>[\w]*)/?', ...),

views.py:

def course_list(request, location, month = None, something = None):

    if month:
        # do stuff

You really want to have a “base path word” as first item in your url.

If you do this:

url(r^(?P<location>[\w]*) ... ),

That will match all urls pretty much

Leave a comment