43
我有以下形式的代碼:如何在模板中顯示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__']