我希望在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的新手。
答案 0 :(得分:1)
你应该更好地使用django formsets 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>