I need help with form which looks like a Gmail inbox and have multiple actions. There is a list of items and I want to wrap it with form, on the way that every item have checkbox in the front of the line. So when user select few items he is able to click on two buttons with different actions for example delete and mark read.
<form action="">
{% for item in object_list %}
<input type="checkbox" id="item.id">
{{ item.name }}
{% endfor %}
<button type="submit" name="delete">Delete</button>
<button type="submit" name="mark_read">Mark read</button>
</form>
I can find which submit button is user click on if use if 'delete' in request.POST
but I cant refer to any form because Django form cant be defined with unknown number of fields as I think. So how can I process selected items in view?
if request.method == 'POST':
form = UnknownForm(request.POST):
if 'delete' in request.POST:
'delete selected items'
if 'mark_read' in erquest.POST:
'mark selected items as read'
return HttpResponseRedirect('')
You may check out the related API usage on the sidebar. You may also want to check out all available functions/classes of the module django.forms , or try the search function .
In Django, we can use class-based views to generate forms without defining one in forms.py. This is often fine, but Django chooses the form widgets for us. Sometimes, we need a bit more control.
The following are 30 code examples for showing how to use django.forms.CheckboxSelectMultiple () . These examples are extracted from open source projects.
A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: In this case, the user needs to be able to add multiple members to a meal. If the choices were fixed and the same for every user, then we could use django-multiple-select-field.
Multiple checkboxes with the same name are all the same field.
<input type="checkbox" value="{{item.id}}" name="choices">
<input type="checkbox" value="{{item.id}}" name="choices">
<input type="checkbox" value="{{item.id}}" name="choices">
you can collect and aggregate them with a single django form field.
class UnknownForm(forms.Form):
choices = forms.MultipleChoiceField(
choices = LIST_OF_VALID_CHOICES, # this is optional
widget = forms.CheckboxSelectMultiple,
)
Specifically, you can use a ModelMultipleChoiceField.
class UnknownForm(forms.Form):
choices = forms.ModelMultipleChoiceField(
queryset = queryset_of_valid_choices, # not optional, use .all() if unsure
widget = forms.CheckboxSelectMultiple,
)
if request.method == 'POST':
form = UnknownForm(request.POST):
if 'delete' in request.POST:
for item in form.cleaned_data['choices']:
item.delete()
if 'mark_read' in request.POST:
for item in form.cleaned_data['choices']:
item.read = True; item.save()
I can't comment Thomas solution so I do it here.
For ModelMultipleChoiceField the argument name is not choices but queryset.
So to take the last example:
class UnknownForm(forms.Form):
choices = forms.ModelMultipleChoiceField(
choices = queryset_of_valid_choices, # not optional, use .all() if unsure
widget = forms.CheckboxSelectMultiple,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With