gpt4 book ai didi

python - 为什么我在 Django formset_factory 中得到单个字段值?

转载 作者:行者123 更新时间:2023-12-04 03:49:50 25 4
gpt4 key购买 nike

当我检查 formset_factory 验证并从 formset 获取字段值时,它只从 formset 返回一个字段值。但是我在我的代码中找不到任何问题。请帮我解决问题。
我的form.py:

class SbTitleForm(forms.Form):
sb_title = forms.CharField(required=False)
sb_permission = forms.BooleanField(required=False)

SbTitleFormSet = formset_factory(SbTitleForm, extra=0)
我的view.py:
def menuuser(request):
sbtitle = SbTitle.objects.all().values()
sbtitle_form = SbTitleFormSet(initial=sbtitle)
if request.method == 'POST':
sbtitle_form = SbTitleForm(request.POST, initial=sbtitle)
if sbtitle_form.is_valid():
print(sbtitle_form.cleaned_data)

context = {
'sbtitle_form':sbtitle_form,
}
return render(request, 'admins/user_role/user_permission.html', context)
我的 HTML:
{% extends 'base/base.html' %} 
{% load static %}
{% block content %}
<div class="card">
<form class="form-horizontal" action="" method="post">
{% csrf_token %}
{{ sbtitle_form.management_form }}
{{ sbitleelement_form.management_form }}
<div id="DataTables_Table_2_wrapper" class="dataTables_wrapper no-footer">
<div class="datatable-scroll">
<table class="table table-bordered table-hover datatable-highlight dataTable no-footer" id="DataTables_Table_2" role="grid" aria-describedby="DataTables_Table_2_info">
<thead>
<tr role="row" class="bg-teal-400">
<th class="sorting text-center h5" tabindex="0" aria-controls="DataTables_Table_2" rowspan="1" colspan="1" aria-label="Job Title: activate to sort column ascending">Sidebar Title</th>
</tr>
</thead>
<tbody>
{% for field in sbtitle_form %}
<tr role="row" class="odd">
<td class="sorting_1 text-center"><h4>{{ field.sb_title.value }} {{ field.sb_permission }}<p class="text-danger">{{ field.errors.as_text }}</p></h4></td>
{% endfor %}
</tbody>
</table>
</div>
<div class="card-footer">
<button class="btn btn-lg btn-primary" type="submit">Save</button>
</div>
</div>
</form>
</div>
{% endblock %}
当我尝试 print(sbtitle_form.cleaned_data) 时,它只返回一个字段值,例如 {'sb_title': '', 'sb_permission': False} 。我无法在表单集中获取其他字段值。请帮我解决问题。

最佳答案

sbtitle_form = SbTitleForm(request.POST, initial=sbtitle)应该是:sbtitle_form = SbTitleFormSet(request.POST, initial=sbtitle)

def menuuser(request):
sbtitle = SbTitle.objects.all().values()
sbtitle_form = SbTitleFormSet(initial=sbtitle)
if request.method == 'POST':
sbtitle_form = SbTitleFormSet(request.POST, initial=sbtitle) # <----
if sbtitle_form.is_valid():
print(form.cleaned_data)
context = {
'sbtitle_form':sbtitle_form,
}
return render(request, 'admins/user_role/user_permission.html', context)
如果您想访问每个表单:
def menuuser(request):
sbtitle = SbTitle.objects.all().values()
sbtitle_form = SbTitleFormSet(initial=sbtitle)
if request.method == 'POST':
sbtitle_form = SbTitleFormSet(request.POST, initial=sbtitle)
if sbtitle_form.is_valid():
for form in sbtitle_form.forms: # <----
print(form.cleaned_data)
context = {
'sbtitle_form':sbtitle_form,
}
return render(request, 'admins/user_role/user_permission.html', context)

关于python - 为什么我在 Django formset_factory 中得到单个字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64593303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com