Is there a way to have a default parameter passed to a action in the case where the regex didnt match anything using django?
urlpatterns = patterns('',(r'^test/(?P<name>.*)?$','myview.displayName')) #myview.py def displayName(request,name): # write name to response or something
I have tried setting the third parameter in the urlpatterns to a dictionary containing ‘ and giving the name parameter a default value on the method, none of which worked. the name parameter always seems to be None. I really dont want to code a check for None if i could set a default value.
Clarification: here is an example of what i was changing it to.
def displayName(request,name='Steve'): return HttpResponse(name) #i also tried urlpatterns = patterns('', (r'^test/(?P<name>.*)?$', 'myview.displayName', dict(name='Test') ) )
when i point my browser at the view it displays the text ‘None’
Any ideas?
The problem is that when the pattern is matched against ‘test/’ the groupdict captured by the regex contains the mapping ‘name’ => None:
This means that when the view is invoked, using something I expect that is similar to below:
which is equivalent to:
for ‘test/’, meaning that name is assigned None rather than not assigned.
This leaves you with two options. You can:
For example:
When taking the second approach, you can simply call the method without the capture pattern, and let python handle the default parameter or you can call a different view which delegates.