Skip to main content

django-json-ld

This is a django template tag to help developers render structured data tags inside their django templates.

Furthermore a collection of views and mixins are included:

  • JsonLdContextMixin, JsonLdView
  • JsonLdSingleObjectMixin, JsonLdDetailView

Installation

Install using pip:

pip install django-json-ld

Add django_json_ld to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # Other apps...
    'django_json_ld',
]

Settings

You can override the following options in settings.py:

JSON_LD_CONTEXT_ATTRIBUTE: the context attribute name used in django_json_ld's Class-Based Views (CBV). Defaults to 'sd'.

JSON_LD_MODEL_ATTRIBUTE: the model attribute name used by JsonLdDetailView to get the model's structured data. Defaults to 'sd'.

JSON_LD_DEFAULT_CONTEXT: default json-ld context when using django_json_ld's CBVs. Defaults to 'https://schema.org/'.

JSON_LD_INDENT: indent are used by json.dumps function to prettify output. Valid values are None or a non-negative integer. Defaults to None - outputs on a single line. This setting is used only if DEBUG is True

JSON_LD_DEFAULT_TYPE: default json-ld type when using django_json_ld's CBVs. Defaults to 'Thing'.

JSON_LD_GENERATE_URL: generate json-ld's url field when using django_json_ld's CBVs. Defaults to True.

JSON_LD_EMPTY_INPUT_RENDERING: behavior when the template tag receives an empty object. Possible values:

  • strict default, raise TemplateSyntaxError.
  • silent renders nothing.
  • generate_thing creates object with URL of current page. eg:
structured_data = {
                "@context": <JSON_LD_DEFAULT_CONTEXT>,
                "@type": <JSON_LD_DEFAULT_TYPE>,
                "url": <current_page_url>,
                }

Usage Example

Assuming you have a structured data sd like the following in your context (copied from the link above).

sd = {
  "@context": "https://schema.org/",
  "@type": "Organization",
  "url": "http://www.example.com/",
  "name": "Unlimited Ball Bearings Corp.",
  "contactPoint": {
    "@type": "ContactPoint",
    "telephone": "+1-401-555-1212",
    "contactType": "Customer service"
    }
}

Then, in your template:

{% load render_json_ld from json_ld %}
{% render_json_ld sd %}

Would render into:

<script type="application/ld+json">
{
  "@context": "https://schema.org/",
  "@type": "Organization",
  "url": "http://www.example.com/",
  "name": "Unlimited Ball Bearings Corp.",
  "contactPoint": {
    "@type": "ContactPoint",
    "telephone": "+1-401-555-1212",
    "contactType": "Customer service"
  }
}
</script>

Class-Based View example

Simple View

views.py

from django_json_ld.views import JsonLdContextMixin

class HomeView(JsonLdContextMixin, generic.ListView):
    structured_data = {
        "@type": "Organization",
        "name": "The Company",
        "description": _("A great company."),
    }
    
    def get_structured_data(self):
        structured_data = super(HomeView, self).get_structured_data()
        structured_data["event"] = get_next_event()
        return structured_data

By using {% render_json_ld sd %}, as explained in the previous example, would render into something like:

{
    "@context":"https://schema.org/",    
    "@type":"Organization",
    "name":"The Company",
    "description":"Uma grande empresa.",
    "url":"http://example.org/",
    "event": {
        "@type": "Event",
        "about": ["Hodler","Monet","Munch"],
        "name": "Peindre l'impossible",
        "startDate": "2016-09-15",
        "endDate": "2017-01-22"
    }
}

In the above example JsonLdContextMixin adds sd to HomeView's context. django_json_ld supports lazy translations, hence "description" showing the translated version of its original value.

Detail View

views.py

from django_json_ld.views import JsonLdDetailView

class ProductDetailView(JsonLdDetailView):
    model=Product

models.py

class Product(models.Model):
    name = models.CharField(_('Name'), max_length=255)
    description = models.TextField(_('Description'))
    
    @property
    def sd(self):
        return {
            "@type": 'Product',
            "description": self.description,
            "name": self.name,
        }

By using {% render_json_ld sd %}, as explained previously, would render into something like:

{
    "@context":"https://schema.org/",    
    "@type":"Product",
    "name":"The Product",
    "description":"A great product.",
    "url":"http://example.org/products/1/the-product/"
}

In the above example JsonLdDetailView adds sd to ProductDetailView's context, using Product's own sd property. The url is generated automatically by JsonLdDetailView. This behaviour is configurable through settings.

Metadata

Release files for django-json-ld 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-json-ld 0.0.5
File Size Uploaded
django-json-ld-0.0.5.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-json-ld 0.0.5
File Interpreter ABI Platform
django_json_ld-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 kB

Release files / django-json-ld-0.0.5.tar.gz

Download URL django-json-ld-0.0.5.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c5a33ccdcb697757c5affc88de16892e2d48c4a724e23c48fbf087b2caf0a8a5
BLAKE2b-256 checksum
How to use checksums
a18aa890ed91df92aa87ac60c22d1d4fb45be2225a0778cb4041def4cf25f958
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release files / django_json_ld-0.0.5-py3-none-any.whl

Download URL django_json_ld-0.0.5-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f0d82b002299e457c5329726c0704775bb7433a46dd1ced305bc5de57d962a1
BLAKE2b-256 checksum
How to use checksums
1f70bed88dd20263249c9dd7246bd481e38fbc83e1aea02487d244cdb4081f3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page