如何在模板中显示 Django '__all__' 表单错误? [英] How do I display the Django '__all__' form errors in the template?

查看:24
本文介绍了如何在模板中显示 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__ 的错误而不必在视图中单独提取它?我想避免以下情况:

How can I display the errors from the key __all__ at the template level without having to extract it in the view separately? I want to avoid the following:

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

推荐答案

{{ form.non_field_errors }}

这篇关于如何在模板中显示 Django '__all__' 表单错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆