Django Sekizai

4.1.0 · active · verified Thu Apr 16

Django Sekizai (meaning "blocks" in Japanese) is a Django application that provides enhanced template block functionality. It allows developers to define placeholders where content blocks are rendered and, from various places in sub-templates, append unique content to those blocks. This is particularly useful for managing CSS and JavaScript dependencies across complex template structures, ensuring that media files are placed correctly (e.g., CSS in the head, JS at the bottom) and duplicates are automatically ignored. The library is currently at version 4.1.0 and is actively maintained by the django CMS Association, with regular releases aligning with Django's own release cycle.

Common errors

Warnings

Install

Imports

Quickstart

To get started with django-sekizai, first add 'sekizai' to your `INSTALLED_APPS` and include `'sekizai.context_processors.sekizai'` in your `TEMPLATES` context processors. In your base template, define where your CSS and JavaScript blocks should be rendered using `{% render_block "css" %}` and `{% render_block "js" %}`. Then, in any extending or included templates, use `{% addtoblock "css" %}` and `{% addtoblock "js" %}` to inject content into these defined blocks. Sekizai will ensure unique entries and proper placement.

# settings.py
INSTALLED_APPS = [
    # ...
    'sekizai',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'sekizai.context_processors.sekizai', # Add Sekizai context processor
            ],
        },
    },
]

# base.html (or your main template)
{% load sekizai_tags %}
<!DOCTYPE html>
<html>
<head>
    <title>My App</title>
    {% render_block "css" %}
</head>
<body>
    <header>...</header>
    {% block content %}{% endblock %}
    <footer>...
        {% render_block "js" %}
    </footer>
</body>
</html>

# my_app/templates/my_app/detail.html
{% extends 'base.html' %}
{% load sekizai_tags %}

{% block content %}
    <h1>Welcome</h1>
    <p>This is my content.</p>

    {% addtoblock "css" %}
        <link rel="stylesheet" href="/static/css/detail.css">
    {% endaddtoblock %}

    {% addtoblock "js" %}
        <script src="/static/js/detail.js"></script>
        <script>
            console.log('Detail page loaded!');
        </script>
    {% endaddtoblock %}
{% endblock %}

view raw JSON →