Django userena检查是否设置了照片

时间:2016-09-28 15:33:48

标签: python django

好的,这就是userena的models.py。如果设置了照片,我可以在html中使用模板标签进行检查吗?第一个if语句检查是否上传了照片。

def get_mugshot_url(self):
    """
    Returns the image containing the mugshot for the user.

    The mugshot can be a uploaded image or a Gravatar.

    Gravatar functionality will only be used when
    ``USERENA_MUGSHOT_GRAVATAR`` is set to ``True``.

    :return:
        ``None`` when Gravatar is not used and no default image is supplied
        by ``USERENA_MUGSHOT_DEFAULT``.

    """
    # First check for a mugshot and if any return that.
    if self.mugshot:
        return self.mugshot.url

    # Use Gravatar if the user wants to.
    if userena_settings.USERENA_MUGSHOT_GRAVATAR:
        return get_gravatar(self.user.email,
                            userena_settings.USERENA_MUGSHOT_SIZE,
                            userena_settings.USERENA_MUGSHOT_DEFAULT)

    # Gravatar not used, check for a default image.
    else:
        if userena_settings.USERENA_MUGSHOT_DEFAULT not in ['404', 'mm',
                                                            'identicon',
                                                            'monsterid',
                                                            'wavatar']:
            return userena_settings.USERENA_MUGSHOT_DEFAULT
        else:
            return None

1 个答案:

答案 0 :(得分:1)

您只需在用户个人资料的实例上复制模板中的第一个if语句即可。像

这样的东西
{% if profile.mugshot %}
  profile.mugshot.url
{% endif %}