{"id":7176,"library":"django-sekizai","title":"Django Sekizai","description":"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.","status":"active","version":"4.1.0","language":"en","source_language":"en","source_url":"https://github.com/django-cms/django-sekizai","tags":["django","templates","frontend","css","javascript","blocks","media management","cms"],"install":[{"cmd":"pip install django-sekizai","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core framework dependency, Sekizai is a Django application.","package":"Django","optional":false}],"imports":[{"note":"Used directly in Django templates to enable Sekizai functionality.","symbol":"sekizai_tags","correct":"{% load sekizai_tags %}"},{"note":"Required for custom views or unit tests that render templates, especially in newer Django versions (1.8+) or when `sekizai.context_processors.sekizai` is not in TEMPLATES settings. Using `RequestContext` or not providing SekizaiContext will result in template errors [6, 14].","wrong":"from django.template import RequestContext","symbol":"SekizaiContext","correct":"from sekizai.context import SekizaiContext"},{"note":"The Sekizai application itself needs to be registered in Django's INSTALLED_APPS.","symbol":"sekizai","correct":"INSTALLED_APPS = [\n    # ...\n    'sekizai',\n]"},{"note":"Add this context processor to your TEMPLATES setting to make Sekizai functionality available in all templates rendered with a RequestContext [1, 3, 10].","symbol":"sekizai.context_processors.sekizai","correct":"TEMPLATES = [\n    {\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\n        'APP_DIRS': True,\n        'OPTIONS': {\n            'context_processors': [\n                # ...\n                'sekizai.context_processors.sekizai',\n            ],\n        },\n    },\n]"}],"quickstart":{"code":"# settings.py\nINSTALLED_APPS = [\n    # ...\n    'sekizai',\n]\n\nTEMPLATES = [\n    {\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\n        'APP_DIRS': True,\n        'OPTIONS': {\n            'context_processors': [\n                'django.template.context_processors.debug',\n                'django.template.context_processors.request',\n                'django.contrib.auth.context_processors.auth',\n                'django.contrib.messages.context_processors.messages',\n                'sekizai.context_processors.sekizai', # Add Sekizai context processor\n            ],\n        },\n    },\n]\n\n# base.html (or your main template)\n{% load sekizai_tags %}\n<!DOCTYPE html>\n<html>\n<head>\n    <title>My App</title>\n    {% render_block \"css\" %}\n</head>\n<body>\n    <header>...</header>\n    {% block content %}{% endblock %}\n    <footer>...\n        {% render_block \"js\" %}\n    </footer>\n</body>\n</html>\n\n# my_app/templates/my_app/detail.html\n{% extends 'base.html' %}\n{% load sekizai_tags %}\n\n{% block content %}\n    <h1>Welcome</h1>\n    <p>This is my content.</p>\n\n    {% addtoblock \"css\" %}\n        <link rel=\"stylesheet\" href=\"/static/css/detail.css\">\n    {% endaddtoblock %}\n\n    {% addtoblock \"js\" %}\n        <script src=\"/static/js/detail.js\"></script>\n        <script>\n            console.log('Detail page loaded!');\n        </script>\n    {% endaddtoblock %}\n{% endblock %}","lang":"python","description":"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."},"warnings":[{"fix":"This is intended behavior for deduplication. If you need content to render multiple times, consider dynamic generation or different namespaces.","message":"Sekizai enforces uniqueness of content within a block namespace. If you add the same content (e.g., a `<script>` tag with the exact same `src`) multiple times using `addtoblock`, it will only be rendered once.","severity":"gotcha","affected_versions":"All versions (feature since 0.5)"},{"fix":"Ensure `{% render_block %}` tags are at the top level of your template or within static HTML elements, typically in your base template's `<head>` or `<body>` [2, 3, 18].","message":"`{% render_block %}` tags must not be placed inside other Django template tag blocks (e.g., `{% block %}`, `{% if %}`, `{% for %}`). This can lead to `TemplateSyntaxError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always place `{% addtoblock %}` within a Django `{% block %}`. If overriding a block, ensure `{{ block.super }}` is used within the overridden block if you intend for Sekizai content from intermediate templates to be included [1, 13].","message":"When using `{% addtoblock %}` within an extending template, it must be nested inside a `{% block %}` tag from the parent template. If the parent block is overridden in a child template *without* calling `{{ block.super }}`, the `addtoblock` content will be ignored.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of this limitation when deploying. Consider alternative strategies for compressing Sekizai-managed assets in production if offline compression is critical [4].","message":"The `django-compressor` integration with `django-sekizai` does not support offline compression. This means assets added via Sekizai might not be compressed during a `compress` management command if offline compression is enabled.","severity":"gotcha","affected_versions":"All versions with `django-compressor`"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"1. Add `'sekizai.context_processors.sekizai'` to your `TEMPLATES['OPTIONS']['context_processors']` list in `settings.py`. 2. If rendering manually in views or tests, ensure you pass `SekizaiContext` (e.g., `render(request, 'template.html', context=SekizaiContext())`) [6, 14].","cause":"Sekizai requires its context processor to be active or `SekizaiContext` to be explicitly used for rendering. This error typically occurs when it's missing from `TEMPLATES['OPTIONS']['context_processors']` in `settings.py` or when using `render_to_response` without `SekizaiContext` in a view or test.","error":"You must enable the 'sekizai.context_processors.sekizai' template context processor or use 'sekizai.context.SekizaiContext' to render your templates."},{"fix":"Ensure `{% render_block %}` tags are at the root level of your template or directly within static HTML tags (e.g., `<head>`, `<body>`) and not nested within other dynamic block tags [2, 3, 18].","cause":"This error or similar `Invalid block tag` errors with Sekizai tags often arise when `{% render_block %}` is placed inside another Django template tag that defines a block (like `{% block %}`, `{% if %}`, `{% for %}`).","error":"Invalid block tag: 'render_block', expected 'endblock'"},{"fix":"1. Always wrap `{% addtoblock %}` within a `{% block %}` tag in inherited templates. 2. If overriding, include `{{ block.super }}` to retain content from parent blocks. 3. Remember Sekizai inherently deduplicates content within a namespace; for multiple identical inclusions, consider making them unique or using different namespaces [1, 3, 11, 13].","cause":"Common causes include: 1. `{% addtoblock %}` is not wrapped inside a `{% block %}` in an extending template. 2. A parent block containing `{% addtoblock %}` is overridden in a child template without calling `{{ block.super }}`. 3. Expecting duplicated content, but Sekizai's deduplication feature is active.","error":"Content added with `{% addtoblock %}` is not appearing in the final rendered HTML or is duplicated unexpectedly."}]}