Saving dropdown menu selection as a cookie in Django
I'd like to save a user selection from a dropdown menu as a cookie. I've seen this question asked before but never fully in python. I wish to use these two cookies in running a simple piece of code so there is no need for a database to be used.
Here is my current HTML code:
<form role="form" method="post">
{% csrf_token %}
<div class="form-group">
<label for="dog-names">Choose a dog name:</label>
<select name="dog-names" id="dog-names" onchange="getSelectValue">
<option value="rigatoni">Rigatoni</option>
<option value="dave">Dave</option>
<option value="pumpernickel">Pumpernickel</option>
<option value="reeses">Reeses</option>
<option value="{{ current_name }}"> {{ current_name }}</option>
</select>
<br/>
<label>Current dog: {{ current_name }}</label>
</div>
<button type="submit" value="Submit" class="btn btn-info">Submit</button>
</form>
Python
def cookies_test(request):
template_name = 'cookies.html'
current_name = "Rigatoni" # default name
if request.method == 'GET':
if 'name' in request.COOKIES:
current_name = request.COOKIES['name']
elif request.method == 'POST':
current_name = request.POST.get('name')
response = render(request, 'test.html', {
"current_name": current_name
})
response.set_cookie('name', current_name)
return response
The python works if I give a value to {{ current_name }} All I want is to be able to save a value from a dropdown menu in a variable so I can save it as a cookie
Any advice at all would be appreciated :)