gpt4 book ai didi

python - 没有显示任何错误的 Django 表单无效

转载 作者:太空宇宙 更新时间:2023-11-04 05:45:56 24 4
gpt4 key购买 nike

下面描述的表格无效。表达式 {{ search_id_form.errors }} 没有显示任何内容。Django 模板:

        <form method="POST">
{% csrf_token %}
{{ search_id_form.idtype.label_tag }}
{{ search_id_form.idtype }}
{{ search_id_form.index.label_tag }}
{{ search_id_form.index }}<br>
<input type="submit" name="id_search_button" value="Submit">
</form>

Python 类:

class IDSearchForm(forms.Form):

idtype = forms.ChoiceField(
choices=[('idx', 'Our Database ID'), ('uprot', 'UniProt'), ('ncbi', 'NCBI')],
initial='idx',
widget=forms.RadioSelect,
label="Which identifier to use:"
)

index = forms.CharField(label="Identifier:")

查看:

def search(request):
if request.method == 'POST':

# handling other forms ...

# find a toxin by id
if 'id_search_button' in request.POST:
search_id_form = IDSearchForm()
if search_id_form.is_valid():
idtype = search_id_form.cleaned_data['idtype']
index = search_id_form.cleaned_data['index']
return render(request, 'ctxdb/result_ctx.html', {
# here I try to use predefined object to pass to renderer (for debugging)
'ctx': get_object_or_404(CTX, idx='21')
})

# handling other forms ...

# other forms
search_id_form = IDSearchForm()
# other forms

return render(request, 'ctxdb/search.html', {
# other forms
'search_id_form': search_id_form,
# other forms
})

在 View 函数中,我在单个页面上处理四种不同的形式。其他表格工作正常。这里有什么问题?

最佳答案

调用 .is_valid 时,您需要将数据传递给 search_id_form,而您并没有这样做。

改变

search_id_form = IDSearchForm()

search_id_form = IDSearchForm(request.POST)

关于python - 没有显示任何错误的 Django 表单无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32316692/

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