I am a new to the Django framework.
I have created a simple arithmetic application using django.
As suggested in the django documentation, I wrote
{% csrf_token %}
in my template file.
But, the thing i notice is that, token value is not changing on post request.
It is showing same value every time with expiry of 364 days
So, let me know the settings to change CSRF Token Value in each post request.
Thanks in advance
My template code below
<form action="{{ action }}" method="post">
{% csrf_token %}
<fieldset>
<legend>{{ tag }}:</legend>
Number 1: <input type="text" size="10" name="num1" value="{{ n1|default:0 }}"><br>
<span> {{ operator }} </span><br>
Number 2: <input type="text" size="10" name="num2" value="{{ n2|default:0 }}"><br>
<span> = </span><br>
Result : <input type="text" size="10" value="{{ res|default:0 }}" disabled>
<span>{{ warning|default:'' }} </span><br><br>
<input type="submit" size="10">
</fieldset>
</form>
<a href="/app1">App1 Home</a>
My view code below
def add(request):
warn = ''
res, n1, n2 = (0, 0, 0)
try:
n1 = int(request.POST['num1'])
n2 = int(request.POST['num2'])
res = n1 + n2
except (ValueError, TypeError):
warn = 'Text data is not allowed.'
params = {'operator': '+', 'action': 'add', 'tag': 'Addition' ,'n1': n1, 'n2': n2, 'res': res, 'warning': warn}
return render(request, 'arithmatic_app1.html', params)