问题:django模板中的“ none”是什么意思?

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

这是我目前拥有的:

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

在上面的示例中,我将用什么来替换“空”?

I want to see if a field/variable is none within a Django template. What is the correct syntax for that?

This is what I currently have:

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

In the example above, what would I use to replace “null”?


回答 0

None, False and True所有这些都可在模板标记和过滤器中找到。None, False,空字符串('', "", """""")和空列表/元组False都由进行求值if,因此您可以轻松地执行

{% 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, False and True all are available within template tags and filters. None, False, the empty string ('', "", """""") and empty lists/tuples all evaluate to False when evaluated by if, so you can easily do

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

A hint: @fabiocerqueira is right, leave logic to models, limit templates to be the only presentation layer and calculate stuff like that in you model. An example:

# 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 }}

Hope this helps :)


回答 1

您还可以使用其他内置模板 default_if_none

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

You can also use another built-in template default_if_none

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

回答 2

is运算符:Django 1.10中的新增功能

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

isoperator : New in Django 1.10

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

回答 3

看看yesno助手

例如:

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

Look at the yesno helper

Eg:

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

回答 4

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

if在Python一般对待NoneFalse''[]{},…所有为假。

{% if profile.user.first_name %} works (assuming you also don’t want to accept '').

if in Python in general treats None, False, '', [], {}, … all as false.


回答 5

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

如果value的值为False(例如None,一个空字符串,0,False);默认显示为“-”。

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

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

You can also use the built-in template filter default:

If value evaluates to False (e.g. None, an empty string, 0, False); the default “–” is displayed.

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

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


回答 6

您可以尝试以下方法:

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

这样,您实际上是在检查表单字段first_name是否具有与其关联的任何值。见{{ field.value }}循环遍历Django文档形式的领域

我正在使用Django 3.0。

You could try this:

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

This way, you’re essentially checking to see if the form field first_name has any value associated with it. See {{ field.value }} in Looping over the form’s fields in Django Documentation.

I’m using Django 3.0.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。