【问题标题】:What is the equivalent of "none" in django templates?django 模板中“none”的等价物是什么?
【发布时间】:2012-08-10 07:59:05
【问题描述】:

我想查看 Django 模板中的字段/变量是否为空。正确的语法是什么?

这是我目前拥有的:

{% if profile.user.first_name is null %}
  <p> -- </p>
{% elif %}
  {{ profile.user.first_name }} {{ profile.user.last_name }}
{% endif%}

在上面的例子中,我会用什么来代替“null”?

【问题讨论】:

    标签: python django django-forms django-templates django-views


    【解决方案1】:

    None, False and True 都在模板标签和过滤器中可用。 None, False,空字符串('', "", """""")和空列表/元组在if评估时都评估为False,所以你可以轻松做到

    {% if profile.user.first_name == None %}
    {% if not profile.user.first_name %}
    

    提示:@fabiocerqueira 是对的,将逻辑留给模型,将模板限制为唯一的表示层,并在您的模型中计算类似的东西。一个例子:

    # someapp/models.py
    class UserProfile(models.Model):
        user = models.OneToOneField('auth.User')
        # other fields
    
        def get_full_name(self):
            if not self.user.first_name:
                return
            return ' '.join([self.user.first_name, self.user.last_name])
    
    # template
    {{ user.get_profile.get_full_name }}
    

    希望这会有所帮助:)

    【讨论】:

    • 我只是尝试将 None 作为参数传递给 {% cache %} 标记并得知它不可用。忙于解决这个问题。
    • {% if profile.user.first_name is None %} 导致 Django 模板出现语法错误。
    • 您的提示通过将 HTML 放入您的模型中来混合表示和逻辑,这与您尝试教授的内容完全相反。如果没有名称(数据模型的逻辑行为),然后在模板中使用default_if_none 过滤器,如何返回 None ?
    • @JasperBryant-Greene 你是对的!刚刚更新。好抓人!谢谢!
    【解决方案2】:

    您也可以使用另一个内置模板default_if_none

    {{ profile.user.first_name|default_if_none:"--" }}
    

    【讨论】:

    • 知道如何将它与日期等其他过滤器一起使用吗?例如,如果日期不存在,是否可以显示“N/A”,但要对其进行格式化?喜欢:{{ post.pub_date|default_if_none:"N/A"|date:"Y-m-d" }}?
    • @AndreasBergström 在您的情况下,您首先申请date。如果发生任何错误,它将返回 None。然后你申请default_if_none
    • 这是引入了什么版本的Django?
    【解决方案3】:

    您也可以使用内置的模板过滤器default

    如果 value 的计算结果为 False(例如 None、空字符串、0、False);显示默认的“--”。

    {{ profile.user.first_name|default:"--" }}
    

    文档: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#default

    【讨论】:

    • default_if_none 可能是更好的选择。
    【解决方案4】:

    isoperator : Django 1.10 中的新功能

    {% if somevar is None %}
      This appears if somevar is None, or if somevar is not found in the context.
    {% endif %}
    

    【讨论】:

      【解决方案5】:

      查看yesno 助手

      例如:

      {{ myValue|yesno:"itwasTrue,itWasFalse,itWasNone" }}
      

      【讨论】:

        【解决方案6】:

        {% if profile.user.first_name %} 有效(假设您也不想接受'')。

        Python 中的if 通常将NoneFalse''[]{}……都视为错误。

        【讨论】:

          【解决方案7】:

          只是关于以前答案的注释:如果我们想显示一个,一切都是正确的 字符串,但如果要显示数字,请注意。

          特别是当您的值为 0 时,bool(0) 的计算结果为 False,因此它不会显示并且可能不是您想要的。

          这种情况下更好用

          {% if profile.user.credit != None %}
          

          【讨论】:

            【解决方案8】:

            你可以试试这个:

            {% if not profile.user.first_name.value %}
              <p> -- </p>
            {% else %}
              {{ profile.user.first_name }} {{ profile.user.last_name }}
            {% endif %}
            

            这样,您实际上是在检查表单字段first_name 是否有任何与之关联的值。请参阅Looping over the form's fields in Django Documentation 中的{{ field.value }}

            我正在使用 Django 3.0。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-01-10
              • 1970-01-01
              • 2016-03-17
              • 2011-04-30
              • 2010-10-03
              • 1970-01-01
              • 2012-04-04
              • 1970-01-01
              相关资源
              最近更新 更多