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 }}
希望这可以帮助 :)