Django表单与未知数量的复选框字段和多个操作 [英] Django form with unknown number of checkbox fields and multiple actions

查看:153
本文介绍了Django表单与未知数量的复选框字段和多个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助表单,看起来像一个Gmail收件箱,并有多个操作。有一个项目列表,我想用表单包装,每一个项目都在行前面的复选框。所以当用户选择几个项目时,他可以用不同的动作点击两个按钮,例如删除和标记阅读。

  form action => 
{%for object in object_list%}
< input type =checkboxid =item.id>
{{item.name}}
{%endfor%}
< button type =submitname =delete>删除< / button>
< button type =submitname =mark_read>标记读取< / button>
< / form>

如果使用,如果删除,我可以找到用户点击哪个提交按钮'请求.POST 但是我不能引用任何形式,因为Django表单不能像未知的字段一样被定义为我想的。那么如何处理所选项目?

  if request.method =='POST':
form = UnknownForm(request.POST):
如果'delete'在request.POST:
'删除所选项目'
如果'mark_read'在erquest.POST:
'标记所选项目如'
return HttpResponseRedirect('')


解决方案

p>具有相同名称的多个复选框都是相同的字段。

 < input type =checkboxvalue ={ {item.id}}name =choices> 
< input type =checkboxvalue ={{item.id}}name =choices>
< input type =checkboxvalue ={{item.id}}name =choices>

您可以使用单个django表单字段收集并汇总。

  class UnknownForm(forms.Form):
choices = forms.MultipleChoiceField(
choices = LIST_OF_VALID_CHOICES,#这是可选的
widget = forms.CheckboxSelectMultiple,

具体来说,您可以使用ModelMultipleChoiceField。






choice = forms.ModelMultipleChoiceField(
queryset = queryset_of_valid_choices,#not可选,使用.all()如果不确定
widget = forms.CheckboxSelectMultiple,


如果request.method =='POST':
form = UnknownForm request.POST):
如果在request.POST中为'delete'.POST:
for form.cleaned_data ['choices']:
item.delete()
如果'mark_read 'in request.POST:
for form.cleaned_data ['choice s']:
item.read = True; item.save()


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('')

解决方案

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()

这篇关于Django表单与未知数量的复选框字段和多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆