Skip to main content

django-templates

django-templates is a Django app that provides a centralized registry for accessing the dictionary of templates used in a request. It also provides a context processor for accessing the registry.

Installation

Add django_templates to the list of INSTALLED_APPS in your project's settings.py.

INSTALLED_APPS = [
    ...
    django_templates,
    ...
]

Add the templates context processor to your project's settings.py.

TEMPLATES = [
    {
        ...
        "APP_DIRS": True,
        # Optionally add the global templates directory if it is not in an app's templates directory.
        "DIRS": [
            "/path/to/global/templates",
        ],
        ...
        # Add the templates context processor to the list of context processors.
        "OPTIONS": {
            "context_processors": [
                ...
                "django_templates.context_processors.templates",
                ...
            ],
        },
    },
]
...

Then you can use the DJANGO_TEMPLATES setting in settings.py to override the templates of apps using this library.

DJANGO_TEMPLATES = {
    "html": {
        "app_name/index": "app_name/templates/app_name/index.html",
        ...
    }
}

You can also use a filename or Path object to load the templates from a YAML or JSON file.

Set the global THEME variable to the default theme for your project.

The default theme is "html", but you can change it to any other theme provided in the "django_templates/themes" directory.

Themes provided in the "django_templates/themes" directory are:

  • html
  • bootstrap5 (Work in progress)
  • tailwindcss (Work in progress)
THEME = "html"

Set the global GET_THEME_FUNCTION variable to the function that will be used to get the theme for a request. This allows you to implement theming in your app by providing a function that returns the theme for a request.

GET_THEME_FUNCTION = 'myapp.module.get_theme_for_request'

...

Usage

To use the template registry in your app, create the file "your_app/django_templates.py" and define your templates there.

Set the DJANGO_TEMPLATES variable, which should contain your default template dictionary or a filename or Path object.

If passing a filename it should be an absolute path to a YAML or JSON file containing the template dictionary.

DJANGO_TEMPLATES = {
    "html": {
        "app_name/index": "app_name/templates/app_name/index.html"
    }
    ...
}

Accessing the registry in your templates

You can access the registry in your templates by using the template filter from the django_templates template tag library. The templates filter requires the request object to be passed to it, which is available in the template context if you have added the django_templates.context_processors.templates context processor to your project's settings.py or a theme name.

{% extends base_template %}
...
{% load templates %}
{{ "template_name"|template:request }}
...

Accessing the registry un your views

from django.shortcuts import render
from django_templates.utils import get_template

def my_view(request):
    template_name = get_template("template_key", request=request)
    ...
    render(request, template_name, context)

Getting the theme for a request

from django_templates.utils import get_theme

current_theme = get_theme(request)

Accessing the registry in your app

You can access the registry in your app by using the get_template_name function from the django_templates.utils module.

An optional theme argument can be passed to the function to get the template name for a specific theme. You can also pass a request object to the function to get the template name for the current theme.

from django_templates.utils import get_template

template_name = get_template("template_key")

Release files for dj-templates 0.0.15

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

Source distribution (sdist)

Source distribution for dj-templates 0.0.15
File Size Uploaded
dj_templates-0.0.15.tar.gz 946.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dj-templates 0.0.15
File Interpreter ABI Platform
dj_templates-0.0.15-py3-none-any.whl Python 3 none any Details

Total release size: 1.9 MB

Release files / dj_templates-0.0.15.tar.gz

Download URL dj_templates-0.0.15.tar.gz
Size 946.4 kB
Tags Source
SHA-256 checksum
How to use checksums
1cc6c9f04f32fea776ea6b61d26072eb012741c1209c92db36624c25e44f0072
BLAKE2b-256 checksum
How to use checksums
a7782eb459e59e749748099af0c9b352ceca9f18f6892169ede10e8e71dde6da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.4 CPython/3.14.7 Linux/7.2.0-1-cachyos

Release files / dj_templates-0.0.15-py3-none-any.whl

Download URL dj_templates-0.0.15-py3-none-any.whl
Size 983.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
37f714d63afd0215bf78e6462bdc05fbf197fa769052a3628ad1d4e4003d965e
BLAKE2b-256 checksum
How to use checksums
1bfaa3b005069099381e21088aa9f9892c00f890e9bbc650d93d7a02d8925141
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.4 CPython/3.14.7 Linux/7.2.0-1-cachyos

Release history Release notifications | RSS feed

0.0.26

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.23

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

This release

0.0.15 This release

2 release files

0.0.14

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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