gpt4 book ai didi

django - 如何在模板中显示 Django '__all__' 表单错误?

转载 作者:行者123 更新时间:2023-11-28 19:34:11 26 4
gpt4 key购买 nike

我有以下表单代码:

# forms.py
class SomeForm(forms.Form):
hello = forms.CharField(max_length=40)
world = forms.CharField(max_length=40)

def clean(self):
raise forms.ValidationError('Something went wrong')

# views.py
def some_view(request):
if request.method == 'POST':
form = SomeForm(request.POST)
if form.is_valid():
pass
else:
form = SomeForm()

data = {
'form': form
}
return render_to_response(
'someform.html',
data,
context_instance=RequestContext(request)
)

# someform.html
{{ form.hello }}
{{ form.hello.errors }}

{{ form.world }}
{{ form.world.errors }}

如何在模板级别显示键 __all__ 的错误,而不必在 View 中单独提取它?我想避免以下情况:

    if form.errors.has_key('__all__'):
print form.errors['__all__']

最佳答案

{{ form.non_field_errors }}

关于django - 如何在模板中显示 Django '__all__' 表单错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2525905/

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