DJ-Templates in your apps
To set up DJ-Templates read the Getting started guide.
When Dj-Templates is set up, create a djtt.py with the variable TEMPLATES in your app directory. The TEMPLATES-variable shall be either a dictionary or a string. If the TEMPLATES variable is a string it is interpreted as a relative path, originating from the app directory, pointing to an YAML or JSON file containing the templates dictionary.
Alternatively you can create a templates.yml, templates.yaml or templates.json which translates to a TEMPLATES dictionary.
The TEMPLATES dictionary
TEMPLATES = {
# Start out with the HTML theme, to which all themes shall extend.
# You can then also provide our own bootstrap5 themed templates.
"html": {
"templates": { # set up the templates for your app
# Point your lookup name of the template to the template name.
# You can also override the base template and set the
# "base_template" variable in your context for your views.
"my_app/base": "my_app/html/base.html.django",
"my_app/index": "my_app/html/index.html.django",
"my_app/form": "my_app/html/form.html.django",
},
"static_files": {
# point your static-file lookup name to your static file
"my_app/css": "my_app/css/my_app.css".
"my_app/js": "my_app/js/my_app.js",
},
},
}
Accessing templates
To access themed templates in your app overload the ThemeMixin class and if using
HTMX the HtmxMixin class. Then set the template_name class variable. If the
variable starts with an @ the template name is looked up in the current theme.
If the template_name name variable starts with an ! the template name variable
shall have the format theme:template_key and it is looked up in the theme set by
the variable.
When using HTMX you can set up a hx_template_name variable, which has the same
format as the template_name variable.
from django.shortcuts import render
from django.views import View
from dj_templates.mixin import HtmxMixin, ThemeMixin
class MyView(HtmxMixin, ThemeMixin, View):
"""My View class"""
template_name = "@my_app/index"
def get_context_data(self, **kwargs):
kwargs.setdefault("base_template", self.get_template_name("@my_app/base"))
return super().get_context_data(**kwargs)
def get(self, request):
return render(request, self.get_template_name(), self.get_context_data())
Including themed templates
To include templates as snippets in your templates, load the templates filters. Use the template filter with the theme variable, which is set by the templates context processor.
{% extends base_template %}
{% load templates %}
{% block content %}
{% include "my_app/snippet"|template:theme %}
{% endblock content %}
Content
Django Templates © 2026 Christian Moser - All rights reserved. - BSD 1-Clause License