【发布时间】:2019-10-27 19:55:44
【问题描述】:
我试图在 zip 中使用 formset_factory 和我的数据库中的一些东西并将其用于模板 这是我的表格:
class class_attandance_form(forms.ModelForm):
choise1 = [(True, 'ح'),
(False, 'غ')]
choise2 = [('مثبت', 'مثبت'),
('منفی', 'منفی')]
attendance = forms.ChoiceField(required=True, choices=choise1)
activity = forms.ChoiceField(required=False, choices=choise2)
score = forms.CharField(required=False, max_length=3)
user = forms.CharField(required=False,max_length=20)
class Meta:
model = Class_Book
fields = ('activity','attendance','score','user')
这是我的观点:
def class_attendance(request):
term = Term.objects.filter(lesson__in=[1,])
form1 = formset_factory(class_attandance_form,max_num=len(term),extra=len(term))
form = form1()
term_form = zip(list(term), list(form))
if request.method == 'POST':
query = form1(request.POST or None)
if query.is_valid():
q = query.cleaned_data
print(q)
return render(request,'teacher/class_attendance.html',{'term_form':term_form})
我的体温是:
<form role="form" method="post">
{% csrf_token %}
<table cellspacing="0" class="table">
<thead>
<tr>
<th>ردیف</th>
<th>نام و نام خانوادگی</th>
<th>حضور و غیاب</th>
<th colspan="2">نمره</th>
</tr>
</thead>
<tbody>
{% for term, form in term_form %}
<tr class="class_book" id="{{ term.id }}">
<td>{{ forloop.counter }}</td>
<td>{{ term.student.first_name }} {{ term.student.last_name }}</td>
<td>
{{ form.attendance }}
</td>
<td>
{{ form.activity }}
</td>
<td>
{{ form.score }}
</td>
</tr>
{{ form.user }}
<script>
$('#id_form-{{ forloop.counter0 }}-user').val({{ term.id }})
</script>
{% endfor %}
</tbody>
</table>
<button id="submit" class="button" type="submit">ثبت</button>
</form>
为什么当我使用query = form1(request.POST or None) 时出现此错误
“使用 Formset 前缀时缺少 ManagementForm 数据”
你能告诉我{{formset.managment_form}} 的用途吗
【问题讨论】:
-
就是为了解决这个问题。为什么不用它?
-
我该如何使用它??
-
...放到模板里?
-
dud 知道我必须把它放在哪里,但我的模板中没有 formset 我有 zip 以及如何在这个特定代码中使用 formset.management_form
-
那么你为什么不直接传递表单集呢?
标签: django django-forms