前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >django-gravatar 使用用户信息生成头像(gravatar 加速)

django-gravatar 使用用户信息生成头像(gravatar 加速)

作者头像
卓越笔记
发布2023-02-18 10:56:01
4160
发布2023-02-18 10:56:01
举报
文章被收录于专栏:卓越笔记

django-gravatar

A lightweight django-gravatar app. Includes helper methods for interacting with gravatars outside of template code.

If you like this library and it's saved you some time, please consider supporting further development with a Gittip donation!

Features

  • Helper methods for constructing a gravatar url and checking an email for an existing gravatar
  • Templatetags for generating a gravatar url or gravatar <img> tag.
  • Full test suite!

Installing

Install from PyPi:

You can pip install the app directly from GitHub:

代码语言:javascript
复制
$ pip install git+git://github.com/twaddington/django-gravatar.git#egg=DjangoGravatar

Alternatively, you can now install directly from PyPi!

代码语言:javascript
复制
$ pip install django-gravatar2

Make sure you install django-gravatar2 as there are several other incompatible django-gravatar libraries available.

Add django_gravatar to your INSTALLED_APPS in settings.py:

代码语言:javascript
复制
INSTALLED_APPS = (
    # ...
    'django_gravatar',
)

# django-gravatar
'''
头像风格类型
404:如果没有任何图像与电子邮件哈希无关,则不加载任何图像,而是返回HTTP 404(找不到文件)响应
mp:(神秘人物)一个人的简单卡通风格的轮廓(不随电子邮件哈希值而变化)
identicon:基于电子邮件哈希的几何图案
monsterid:生成的具有不同颜色,面孔等的“怪物”
wavatar:生成的具有不同特征和背景的面孔
retro:生成的令人敬畏的8位街机风格像素化面孔
robohash:具有不同颜色,面部等的生成的机器人
blank:透明的PNG图像(以下为演示目的添加到HTML的边框)
'''
GRAVATAR_DEFAULT_IMAGE = "wavatar"  # 头像风格,一定要填,不填就是默认的头像
GRAVATAR_DEFAULT_SIZE = 48  # 头像大小
GRAVATAR_DEFAULT_SECURE = True  # If True use https, otherwise plain http
GRAVATAR_URL = "http://sdn.geekzu.org/"  # Gravatar http 头像加速域名,链接后面要带 /
GRAVATAR_SECURE_URL = "https://sdn.geekzu.org/"  # Gravatar https 头像加速域名,链接后面要带 /

Basic Usage

Use in code:

代码语言:javascript
复制
from django_gravatar.helpers import get_gravatar_url, has_gravatar, get_gravatar_profile_url, calculate_gravatar_hash

url = get_gravatar_url('alice@example.com', size=150)
gravatar_exists = has_gravatar('bob@example.com')
profile_url = get_gravatar_profile_url('alice@example.com')
email_hash = calculate_gravatar_hash('alice@example.com')

Use in templates:

代码语言:javascript
复制
{% load gravatar %}

{% gravatar_url user.email 150 %}
# https://secure.gravatar.com/avatar/hash.jpg?size=150

{% gravatar user.email 150 %}
# <img class="gravatar" src="https://secure.gravatar.com/avatar/hash.jpg?size=150" width="150" height="150" alt="" />

{% gravatar user.email 150 "user@example.com" %}
# <img class="gravatar" src="https://secure.gravatar.com/avatar/hash.jpg?size=150" width="150" height="150" alt="user@example.com" />

{% gravatar_profile_url user.email %}
# https://secure.gravatar.com/hash

Configuring

The following options can be configured in your settings.py:

GRAVATAR_URL # Gravatar base url. Defaults to 'http://www.gravatar.com/'

GRAVATAR_SECURE_URL # Gravatar base secure https url. Defaults to 'https://secure.gravatar.com/'

GRAVATAR_DEFAULT_SIZE # Gravatar size in pixels. Defaults to '80'

GRAVATAR_DEFAULT_IMAGE # An image url or one of the following: 'mm', 'identicon', 'monsterid', 'wavatar', 'retro'. Defaults to 'mm'

GRAVATAR_DEFAULT_RATING # One of the following: 'g', 'pg', 'r', 'x'. Defaults to 'g'

GRAVATAR_DEFAULT_SECURE # True to use https by default, False for plain http. Defaults to True

gravatar.com -> 

Django Image Requests

Original submission from, and big thanks to Paul Kenjora. Modified and improved by Carly Stambaugh.

代码语言:javascript
复制
import hashlib
import urllib
from django import template
from django.utils.safestring import mark_safe
 
register = template.Library()
 
# return only the URL of the gravatar
# TEMPLATE USE:  {{ email|gravatar_url:150 }}
@register.filter
def gravatar_url(email, size=40):
  default = "https://example.com/static/images/defaultavatar.jpg"
  return "https://www.gravatar.com/avatar/%s?%s" % (hashlib.md5(email.lower()).hexdigest(), urllib.urlencode({'d':default, 's':str(size)}))
 
# return an image tag with the gravatar
# TEMPLATE USE:  {{ email|gravatar:150 }}
@register.filter
def gravatar(email, size=40):
    url = gravatar_url(email, size)
    return mark_safe('<img src="%s" height="%d" width="%d">' % (url, size, size))

See Also: django-gravatar project and if you use Python 3 try out libgravatar. Both are third party libraries and not officially supported by Gravatar.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • django-gravatar
    • Features
      • Installing
        • Basic Usage
          • Configuring
            • Django Image Requests
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档