Django - 复选框数组

发布于 2024-12-12 02:16:57 字数 1688 浏览 0 评论 0原文

我想在我的一个模板中取消多个复选框,但无法使其工作。

我试图在我的表单中传递一个包含 forms.BooleanField() 的数组,如下所示:

class ProjetSettingsForm(forms.Form):

    ## A bunch of form fields of no interest

    arrayCheck = []
    cb1 = forms.BooleanField()
    cb2 = forms.BooleanField()
    arrayCheck.append(cb1)
    arrayCheck.append(cb2)

并在我的模板中像这样输出

{% for a in form.arrayCheck %}
{{ a }}
{% endfor %}

我的表单由视图调用:

def settings(request):
    if request.method == "POST" and (request.POST.get("settings_task_type", "") == "Enregistrer Task Type"):
                form = ProjetSettingsForm(request.POST, proj_id=request.session['proj'])
                if form.is_valid():
                        settings = form.save_task_type()
                        c = {'proj':proj, 'form':form, 'settings':settings}
                        return render_to_response('projet/settings.html', c, context_instance=RequestContext(request))

        else:
                form = ProjetSettingsForm(proj_id=request.session['proj'])
                settings = ""

        c = {'proj':proj, 'form':form, 'settings':settings}
        return render_to_response('projet/settings.html', c, context_instance=RequestContext(request))

但它显示:

<django.forms.fields.BooleanField object at 0xb595a2ec> <django.forms.fields.BooleanField object at 0xb595a22c> 

如何让它正确显示复选框?

我需要将复选框作为数组而不是一一传递的原因是,它们会很多,而且我不会提前知道它们的确切数量,这取决于 sql 查询。

编辑:

如果有人看到这篇文章,我找到了解决方案。当我需要传递一系列复选框时,我不使用表单。

我直接从视图发送数组,在模板中创建复选框并在 request.POST.getlist('my_array') 中获取结果

I would like to out a number of checkboxes in one of my templates and I can't get it to work.

I'm trying to pass an array containing forms.BooleanField() in my form like :

class ProjetSettingsForm(forms.Form):

    ## A bunch of form fields of no interest

    arrayCheck = []
    cb1 = forms.BooleanField()
    cb2 = forms.BooleanField()
    arrayCheck.append(cb1)
    arrayCheck.append(cb2)

and output it like this in my template

{% for a in form.arrayCheck %}
{{ a }}
{% endfor %}

My form is called by a view :

def settings(request):
    if request.method == "POST" and (request.POST.get("settings_task_type", "") == "Enregistrer Task Type"):
                form = ProjetSettingsForm(request.POST, proj_id=request.session['proj'])
                if form.is_valid():
                        settings = form.save_task_type()
                        c = {'proj':proj, 'form':form, 'settings':settings}
                        return render_to_response('projet/settings.html', c, context_instance=RequestContext(request))

        else:
                form = ProjetSettingsForm(proj_id=request.session['proj'])
                settings = ""

        c = {'proj':proj, 'form':form, 'settings':settings}
        return render_to_response('projet/settings.html', c, context_instance=RequestContext(request))

But it displays :

<django.forms.fields.BooleanField object at 0xb595a2ec> <django.forms.fields.BooleanField object at 0xb595a22c> 

How can I get it to display the checkboxes correctly ?

The reason I need to pass the checkboxes as an array and not one by one is that there will be many of them and I won't know in advance their exact number, it depends on a sql query.

EDIT:

If someone comes across this post, i found a solution. I don't use a form when I need to pass an array of checkboxes.

I send the array directly from my view, create the checkboxes in the template and get the results in request.POST.getlist('my_array')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世态炎凉 2024-12-19 02:16:57

不要在表单之外使用表单字段。他们不是那样工作的。创建一个带有 MultipleChoiceField 的表单。

Don't use form fields outside a form. They don't work like that. Create a form with a MultipleChoiceField.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文