1

我想在django的views.py中多次接收表单中生成的表的单选按钮值。

我的模板文件类似于:

<form action = "/appName/accept" method = "POST"
    {% for l in list %}
        <table>
        <tr>
        <td>{{l.value}}</td> 
        </>tr
        </table>  
        <input type = "radio" name = "acceptance" value ="accept">Accept</input>
        <input type = "radio" name = "acceptance" value ="reject">Reject</input>
     {% endfor %}
     <input type ="submit" name = "submit">SUBMIT</input>
</form>

这意味着呈现的页面将如下所示:

值 1

  • 接受

  • 拒绝

值 2

  • 接受
  • 拒绝

等等提交按钮

一旦用户单击提交按钮,我想收集每个值的所有单选按钮的值以及表格行,但如何在views.py 中做到这一点?我知道,如果我们给无线电输入提供通用名称,比如这里的“接受”,那么一次只会选择其中一个。请帮忙。我是这个概念和 django 的新手。

4

1 回答 1

2

你应该更好地使用 django 表单集https://docs.djangoproject.com/en/1.8/topics/forms/formsets/

如文档中所述:

from django.forms.formsets import formset_factory
from django.shortcuts import render_to_response
from myapp.forms import ArticleForm

def manage_articles(request):
    ArticleFormSet = formset_factory(ArticleForm)
    if request.method == 'POST':
        formset = ArticleFormSet(request.POST, request.FILES)
        if formset.is_valid():
            for form in formset:
                form.cleaned_data...
                # here you can access each radio button


    else:
        formset = ArticleFormSet()
    return render_to_response('manage_articles.html', {'formset': formset})

manage_articles.html 模板可能如下所示:

<form method="post" action="">
    {{ formset.management_form }}
    <table>
        {% for form in formset %}
        {{ form }}
        {% endfor %}
    </table>
</form>
于 2015-06-14T12:00:46.013 回答