Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form with unknown number of checkbox fields and multiple actions

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('')
like image 314
Goran Avatar asked Nov 13 '13 07:11

Goran


People also ask

Where can I find all available functions in Django forms?

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 .

How to generate a form without a form in Django?

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.

How many code examples are there for Django forms?

The following are 30 code examples for showing how to use django.forms.CheckboxSelectMultiple () . These examples are extracted from open source projects.

What is a manytomany field in Django?

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.


2 Answers

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()
like image 93
Thomas Avatar answered Oct 18 '22 20:10

Thomas


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,
)
like image 6
y0no Avatar answered Oct 18 '22 20:10

y0no