74👍
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.
- [Django]-Multiple many-to-many relations to the same model in Django
- [Django]-How to set the timezone in Django
- [Django]-How to run this code in django template
13👍
Passing a default value to the method makes parameter optional.
In your case you can make:
def calculate(request, b=None)
pass
Then in your template you can use condition for different behaviour:
{% if b %}
Case A
{% else %}
Case B
{% endif %}
👤Kee
- [Django]-Django class-based view: How do I pass additional parameters to the as_view method?
- [Django]-Going from twitter date to Python datetime date
- [Django]-Django urls without a trailing slash do not redirect
- [Django]-How to keep all my django applications in specific folder
- [Django]-Setting up Mimetype when using TemplateView in Django
- [Django]-Django Static Files Development
Source:stackexchange.com