django模板中的“none”是什么?
我想看看一个字段/variables是否在Django模板中没有。 什么是正确的语法?
这是我现在有:
{% if profile.user.first_name is null %} <p> -- </p> {% elif %} {{ profile.user.first_name }} {{ profile.user.last_name }} {% endif%} 在上面的例子中,我会用什么来replace“null”?
  None, False and True全部在模板标签和filter中可用。  None, False ,空值的string( '', "", """""" )和空的列表/元组都通过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 }} 
希望这可以帮助 :)
 您也可以使用另一个内置模板default_if_none 
 {{ profile.user.first_name|default_if_none:"--" }} 
看看yesno的帮手
例如:
 {{ myValue|yesno:"itwasTrue,itWasFalse,itWasNone" }} 
  {% if profile.user.first_name %}作品(假设你也不想接受'' )。 
  if在Python中通常把None , False , '' , [] , {} …都if为false。 
 你不需要做这个“如果”,使用: {{ profile.user.get_full_name }} 
 您也可以使用内置的模板filterdefault : 
如果值的计算结果为False(例如None,一个空string,0,False); 显示默认的“ – ”。
 {{ profile.user.first_name|default:"--" }} 
文档: https : //docs.djangoproject.com/en/dev/ref/templates/builtins/#default
  is运算符:在Django 1.10中is新的 
 {% if somevar is None %} This appears if somevar is None, or if somevar is not found in the context. {% endif %}