Skip to main content

A simple Django module for adding structured data and OpenGraph tags to your views.

Project description

Django Structured Data PyPI

Template tags to assist in adding structured metadata to views and models.

Install

  1. Add "structured_data" to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'structured_data',
]

Use

In models

Define a structured_data property on your models. This is written in a standard JSON-LD format. You can add as much or as little detail as you like - only data that exists will be mapped. Below is a very detailed example.

    @property
    def structured_data(self):
        url = SITE_URL + self.get_absolute_url()
        data = {
            '@type': 'BlogPosting',
            'headline': self.title,
            'description': self.summary,
            'author': {
                '@type': 'Person',
                'name': self.author.name,
                'url': self.author.get_absolute_url(),
            },
            'datePublished': self.created,
            'dateModified': self.modified,
            'url': url,
            'inLanguage': 'en-US',
            'keywords': ['python', 'django'],
            'articleSection': 'Technology',
            'publisher': {
                '@type': 'Organization',
                'name': 'My Site',
            },
            'image': {
                'url': SITE_URL + self.image.url,
                'width': 1200,
                'height': 630,
                'caption': 'A description of the image',
            },
        }
        return data

In templates

JSON-LD

Use the json_ld_for template tag to render your structured data as JSON-LD.

{% load jsonld %}
{% json_ld_for post %}

Open Graph

The og_for tag translates your JSON-LD data to Open Graph meta tags for Facebook, Telegram, and other platforms.

{% load opengraph %}
{% og_for post %}

The following JSON-LD properties are mapped:

JSON-LD Open Graph
name og:title
description og:description
url og:url
image (string or dict) og:image, og:image:width, og:image:height
image.caption og:image:alt

For Article, BlogPosting, and NewsArticle types, additional mappings apply:

JSON-LD Open Graph
headline og:title (overrides name)
datePublished article:published_time
dateModified article:modified_time
author (string or .url) article:author
articleSection article:section
keywords (list) article:tag (one per keyword)

All other types (including Event and its subtypes) receive og:type = website.

HTML Meta Tags

The meta_for tag extracts standard HTML meta tags from your JSON-LD data.

{% load meta %}
{% meta_for post %}
JSON-LD Meta Tag
description <meta name="description">
author (string or .name) <meta name="author">
keywords (list or string) <meta name="keywords">
location (string or .name) <meta name="location">

Twitter Cards

The twitter_for tag generates Twitter Card meta tags.

{% load twitter %}
{% twitter_for post %}
JSON-LD Twitter Tag
@type twitter:card (summary_large_image for articles, events, and recipes; summary otherwise)
image.caption twitter:image:alt
author (string or .name) twitter:creator

Twitter falls back to Open Graph tags for title, description, and image, so og_for should be used alongside twitter_for.

Global tags

Some Open Graph properties like og:site_name and og:locale are site-wide and not specific to any one object. Add these directly in your base template:

<meta property="og:site_name" content="My Site" />
<meta property="og:locale" content="en_US" />

If you want a site-wide JSON-LD identity, add an Organization block in your base template with an @id so individual pages can reference it:

<script type="application/ld+json">
{"@context": "https://schema.org", "@type": "Organization", "@id": "https://example.com/#organization", "name": "My Site", "url": "https://example.com"}
</script>

Then reference it from your model's structured_data using the same @id:

'publisher': {'@id': 'https://example.com/#organization'}

License

This software is released under the MIT license.

Copyright (c) 2019-2026 Luke Rogers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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_structured_data-0.9.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

django_structured_data-0.9.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file django_structured_data-0.9.0.tar.gz.

File metadata

  • Download URL: django_structured_data-0.9.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_structured_data-0.9.0.tar.gz
Algorithm Hash digest
SHA256 9353cfc40f5c76ffe8b5810a5bf012c7a1e75762b02f4bc3577c4055e43ed078
MD5 92f2f5c458a976bf295a757f10a7c44b
BLAKE2b-256 2ab13151d7ac3c4e174cff4fc8d80020d8bafbe79c79c10789262d76526d9193

See more details on using hashes here.

File details

Details for the file django_structured_data-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: django_structured_data-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_structured_data-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 487245607d32f8696a18da4a75661d10ed2b2fbf89aa23375b5bedd042c69db0
MD5 c00c6d808e074af790a933f79a8add88
BLAKE2b-256 462726f242fa6f5bd373b104220661cf7df0c8536350f46d928809e5738058f0

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