Skip to main content

Django template tag for json-ld

Project description


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


Install using pip:

pip install django-json-ld

Add django_json_ld to INSTALLED_APPS in

    # Other apps...


You can override the following options in

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 ''.

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": "",
  "@type": "Organization",
  "url": "",
  "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 django_json_ld %}
{% render_json_ld sd %}

Would render into:

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

Class-Based View example

Simple View

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:

    "name":"The Company",
    "description":"Uma grande empresa.",
    "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

from django_json_ld.views import JsonLdDetailView

class ProductDetailView(JsonLdDetailView):

class Product(models.Model):
    name = models.CharField(_('Name'), max_length=255)
    description = models.TextField(_('Description'))

    def sd(self):
        return {
            "@type": 'Product',
            "description": self.description,

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

    "name":"The Product",
    "description":"A great 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.

Project details

Download files

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

Files for django-json-ld, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size django_json_ld-0.0.4-py3-none-any.whl (7.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-json-ld-0.0.4.tar.gz (5.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page