如何在模板中显示Django'__all__'表单错误?
我有以下表单代码:
# 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__
的错误,而不必在视图中单独提取它? 我想避免以下情况:
if form.errors.has_key('__all__'): print form.errors['__all__']
{{ form.non_field_errors }}
{{ form.non_field_errors }}
与表单相关的错误字段
{{ form.password.errors }}
在这种情况下,与像passoword这样的文本字段有关的错误