Skip to main content

Use Gravatar in a Django Website

Project description

Dj-Gravatar

Current Version

Dj-Gravatar is a Python package to display avatars using Gravatar on a website developed using Django Framework.

Installation

Windows OS

In the terminal/PowerShell, type

python -m pip install dj-gravatar

If you do not have pip, then download the package from here, unzip it and keep it in your Django project's root directory (as an app).

Linux/MacOS

In the terminal, type

sudo pip3 install dj-gravatar

If you are in a virtual environment, type

pip install dj-gravatar

If you do not have pip, then download the package from here, unzip it and keep it in your Django project's root directory (as an app).

Usage

Suppose the directory structure of your project is like this.

Django_Project
│   db.sqlite3
│   manage.py
│
├───pages
│   │   admin.py
│   │   apps.py
│   │   models.py
│   │   tests.py
│   │   urls.py
│   │   views.py
│   │   __init__.py
│   └───templates
│       │   base.html
│       │
│       └───pages
│               about.html
│               index.html
│
└───Django_Project
        asgi.py
        settings.py
        urls.py
        wsgi.py
        __init__.py

In Django_Project/settings.py,

...
INSTALLED_APPS = [
    ...
    'dj_gravatar'   # NOTE: underscore(_) is used instead of hyphen(-)
    ...
]
...

In the Django Template (.html file) where you want to show the gravatar,

{% load gravatar %}
...

<img src="{% gravatar_url 'email@mail.com' 200 %}" alt="profile-pic">
...

The above code outputs like this:

<img src="http://www.gravatar.com/avatar/267f3587edc9b64d8e80ee7eca8abbcb?s=200&d=" alt="profile-pic">
<!-- OR -->
<img src="http://www.gravatar.com/avatar/[hash]?s=200&d=" alt="profile-pic">

The argument s=200 represents the size of the image (size=200px). Default size is 80px.

You can also use the following snippet to achieve the same as above:

{% load gravatar %}
...
{% gravatar 'email@mail' 200 "alt='profile-pic'" %}
...

You can use any HTML attribute instead of alt, like style. But only use one optional attribute at a time, Otherwise you will get TemplateSyntaxError.

If you want to use https://.../ instead of the default http://.../, then add the following setting in your settings.py file.

GRAVATAR_SECURE = True  # Default False

If any email address has no matching Gravatar image, then, a default image is shown. There are the following types of default images:

  • 404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response
  • mp: (mystery-person) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)
  • identicon: a geometric pattern based on an email hash
  • monsterid: a generated 'monster' with different colors, faces, etc
  • wavatar: generated faces with differing features and backgrounds
  • retro: awesome generated, 8-bit arcade-style pixelated faces
  • robohash: a generated robot with different colors, faces, etc
  • blank: a transparent PNG image (border added to HTML below for demonstration purposes)

You can see here how each type of default image looks like.

To show a particular default image out of the above, add the following setting to your settings.py file.

GRAVATAR_DEFAULT_URL = 'identicon'  # Choose any one of the above

Contribute

Contributions are welcome. If you spot any bug(s), have a feature request, or anything else, don't hesitate to do a Pull Request, or opening an issue from here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj-gravatar-0.0.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

dj_gravatar-0.0.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file dj-gravatar-0.0.2.tar.gz.

File metadata

  • Download URL: dj-gravatar-0.0.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.6.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.24.0 CPython/3.7.3

File hashes

Hashes for dj-gravatar-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2d726bd532e6d52aefc7f8f9f33bc50599f54b57ea5ee475ce8aa5e3b41e4531
MD5 bedc9f30cef35519c54b5e24eb699997
BLAKE2b-256 40df8398c4d3d99a89fc8249986cdb8962c8a1ff221e09fe5388ea43f2e54985

See more details on using hashes here.

File details

Details for the file dj_gravatar-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: dj_gravatar-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.6.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.24.0 CPython/3.7.3

File hashes

Hashes for dj_gravatar-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 af92d53c09c3aab0666d307211fc79e515e37d8c9c4065b240faecf0607294db
MD5 ca3b52b9d752ffe8a6c4785d86d2a192
BLAKE2b-256 7b152fceebee24cac0297f99f09839bc66ce5848368ae8daefa844843acbf9ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page