Skip to main content

Simple SEO & meta tag management for Django

Project description

django-snakeoil helps manage your <meta> tags.

Works on all supported Django versions using PostgreSQl, or with any database on Django 3.1+.

It offers full internationalization support (tags for multiple languages), content set from object attributes, automatic Opengraph image width and heights for ImageField, and more.

Getting started

To install, pip install django-snakeoil or use your favourite package manager.

You can use Snakeoil in two ways. If you’d like to attach metadata to an object, you can use the model abstract base class:

from snakeoi.models import SEOModel

class MyModel(SEOModel):
    pass

For situations where you can’t change the model (flatpages, third party apps) or don’t have one at all, there is an SEOPath model that maps paths to your meta tags.

Tags are added in the admin (or however else you like) as JSON. For example:

{
    "default": [
        {"name": "description", "property": "og:description", "content": "Meta description"},
        {"property": "og:title", "content": "My blog post"},
        {"name": "author", "attribute": "author_name"},
        {"property": "og:image", "static": "img/default.jpg"}
    ]
}

Where default will work for any language. You can replace default with a language code, e.g. “nl_NL”, and these tags will only display if the current language is Dutch. This will generate something like:

<meta name="description" property="og:description" content="Meta description">
<meta property="og:title" "content="My blog post">
<!-- from my_object.author_name -->
<meta name="author" content="Tom Carrick">
<!-- build a static URL -->
<meta property="og:image" content="/static/img/default.jpg">

Note that when using static, width and height are not added, but may add these yourself. For ImageField, this will be added automatically:

{
    "default": [
        {"property": "og:image", "attribute": "main_image"}
    ]
}

Results in:

<meta property="og:image" content="/media/blog_1_main_image.jpg">
<meta property="og:image:width" content="640">
<meta property="og:image:height" content="480">

Django Templates

Add snakeoil to your INSTALLED_APPS:

INSTALLED_APPS = [
    "snakeoil",
    # ...
]

In your base template, add this where you want the tags to appear:

{% load snakeoil %}
{% block head %}
    {% meta %}
{% endblock %}

This will automatically find an object based on the get_absolute_url() of your model, by looking in the request context. If nothing is found, snakeoil will check for an SEOPath object for the current path. If you have an object, it is recommended to pass it into the tag directly to short-circuit the tag finding mechanisms:

{% meta my_obj %}

Jinja2

Set your environment:

from jinja2 import Environment
from snakeoil.jinja2 import get_meta_tags

def environment(**options):
    env = Environment(**options)
    env.globals.update(
        {
            "get_meta_tags": get_meta_tags,
            # ...
        }
    )
    return env

In your template:

{% block meta %}
    {% with meta_tags=get_meta_tags() %}
        {% include "snakeoil/seo.jinja2" %}
    {% endwith %}
{% endblock meta %}

To pass in an object:

{% block meta %}
    {% with meta_tags=get_meta_tags(my_object) %}
        {% include "snakeoil/seo.jinja2" %}
    {% endwith %}
{% endblock meta %}

Notes

Thanks to kezabelle for the name. For those wondering:

Metadata is often used for SEO purposes. A lot of people (rightly or not) consider SEO to be snakeoil. Also, SnakEOil.

The old version of django-snakeoil can be found on the old branch, but won’t be updated.

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

django-snakeoil-1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_snakeoil-1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file django-snakeoil-1.0.tar.gz.

File metadata

  • Download URL: django-snakeoil-1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for django-snakeoil-1.0.tar.gz
Algorithm Hash digest
SHA256 b6f7ba7a63f6223d62a07450ec473b314317b624e0f5bdf51d5159f5bd2322df
MD5 4ec31984ea63d7b39e899f2365c3ea81
BLAKE2b-256 96fb4086b87cf61eae7ad26b4421a61483e6422918ecb2421322a11218c8543d

See more details on using hashes here.

File details

Details for the file django_snakeoil-1.0-py3-none-any.whl.

File metadata

  • Download URL: django_snakeoil-1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for django_snakeoil-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 acf824e2e88b7f17c27027ba451bda0f12c5d0052f5578bea6330355012e4523
MD5 cefca45b593d80461322420270fd251a
BLAKE2b-256 e0627a8ed7a9bb35ddf4fd10618baafbd5923105367fd026706dda464c2d9238

See more details on using hashes here.

Supported by

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