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 (string or dict) twitter:image
image.caption twitter:image:alt
author (string or .name) twitter:creator

Twitter falls back to Open Graph tags for title and description, 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.10.0.tar.gz (12.6 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.10.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_structured_data-0.10.0.tar.gz
  • Upload date:
  • Size: 12.6 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.10.0.tar.gz
Algorithm Hash digest
SHA256 acd3c2d6827699df175199929bdf92b0e972e4c33c61c4ea7685dcad7e991083
MD5 5fd284b0643ba0ecd2a8f07e86f9303e
BLAKE2b-256 e56d6c3d879e613e33b77d466a23143b7cc7ece6c8e9ecdabf9f18883d89da15

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_structured_data-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 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.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 821063798ed99a92db77eac8c76b9bbb0b5924b25c56bcfa01b3e199b8ca3b7d
MD5 c6d0ad7757fc0fffcdfee076e01bca7b
BLAKE2b-256 4d5f749356a8b383585fd580ac7b2a06cfb9052c74ca51690b547b071c6a1bb5

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