slypete slypete -4 years ago 221
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?


Answer Source

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'),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download