Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i pass optional arguments in Django view

I have one function in view like

def  calculate(request , b)

I want that this function should work even if b is not passes to it

like image 318
tej.tan Avatar asked Sep 05 '25 16:09

tej.tan


1 Answers

You may also need to update your URL dispatch to handle the request with, or without, the optional parameter.

url(r'^calculate/?(?P<b>\d+)?/?$', 'calculate', name='calculate'),  
url(r'^calculate/$', 'calculate', name='calculate'),

If you pass b via the URL it hits the first URL definition. If you do not include the optional parameter it hits the second definition but goes to the same view and uses the default value you provided.

like image 180
vjimw Avatar answered Sep 07 '25 16:09

vjimw