您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

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

希望这可以帮助 :)

Go 2022/1/1 18:27:55 有309人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶