【问题标题】:Obtaining number of errors in template with template tags使用模板标签获取模板中的错误数
【发布时间】:2019-08-19 03:11:58
【问题描述】:

如果表单没有加载错误,我想使用模板标签仅显示某些 html。我在 def clean() 中有这个:

forms.py

home_zipcode = cleaned_data.get('home_zipcode')
if ' ' in home_zipcode:
    self.add_error('home_zipcode', "Please remove all spaces from the Zip Code.")            
    raise forms.ValidationError('Please review the errors below.')

模板

{% if no errors %}

   some html

{% endif %}

你知道我会用什么模板标签来做这个吗?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您可以检查form.errors 是否为“falsy”,如果字典没有任何条目则为“falsy”

    {% if not form.errors %}
    

    https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#boolean-operators

    【讨论】:

      【解决方案2】:

      知道了。

      {% if form.errors|length == 0 %}
      
        some html
      
      {% endif %}
      

      【讨论】:

        猜你喜欢
        • 2012-01-04
        • 2014-03-20
        • 2017-02-16
        • 2010-11-25
        • 2011-03-04
        • 2022-11-25
        • 2018-10-21
        • 2011-06-15
        • 2011-04-16
        相关资源
        最近更新 更多