slypete slypete - 3 months ago 55
Python Question

Optional get parameters in django?

Can someone please explain how you can write a url pattern and view that allows optional parameters? I've done this successfully, but I always break the url template tag.

Here's what I have currently:


(r'^so/(?P<required>\d+)/?(?P<optional>(.*))/?$', '')


def so(request, required, optional):

If I use the url template tag in this example providing both arguments, it works just fine; however, if I omit the optional argument, I get a reversing error.

How can I accomplish this?



I generally make two patterns with a named url:

url(r'^so/(?P<required>\d+)/$', '', name='something'),
url(r'^so/(?P<required>\d+)/(?P<optional>.*)/$', '', name='something_else'),