12๐
โ
As Daniel mentioned, you have to add a name
attribute to form elements, so they are submitted to the server.
index.html
<form method="post">
{% csrf_token %}
<div class="form-check">
<input class="form-check-input" type="checkbox" value="Apple" id="apple" name="fruits">
<label class="form-check-label" for="apple">Apple</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="Mango" id="mango" name="fruits">
<label class="form-check-label" for="mango">Mango</label>
</div>
<button type="submit">Submit</button>
</form>
That way, you can get a list of fruits in your view:
views.py
if request.method == 'POST':
fruits = request.POST.getlist('fruits')
The fruits
variable would be a list of check inputs. E.g.:
['Apple', 'Mango']
๐คVitor Freitas
1๐
input
elements need a name
attribute, otherwise the browser does not send any data.
๐คDaniel Roseman
- [Django]-How to assign GROUP to user from a registration form (for user) in Django?
- [Django]-How to extend UserCreationForm with fields from UserProfile
- [Django]-How to serve django static files on heroku with gunicorn
- [Django]-Use pyExcelerator to generate dynamic Excel file with Django. Ensure unique temporary filename
Source:stackexchange.com