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

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.12
File Size Uploaded
dj_templates-0.0.12.tar.gz 940.3 kB Details

Built distribution (wheel)

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

Total release size: 1.9 MB

Release files / dj_templates-0.0.12.tar.gz

Download URL dj_templates-0.0.12.tar.gz
Size 940.3 kB
Tags Source
SHA-256 checksum
How to use checksums
17a2a31fcf868a87cc8299877975e7710a195fdda4a1a455b97b329493dfb3e1
BLAKE2b-256 checksum
How to use checksums
f70116804aedd209ec9d4580da3e655eb86e6dfd1813ce1fed7ce81df33873f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.4 CPython/3.14.6 Linux/7.1.8-1-cachyos

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

Download URL dj_templates-0.0.12-py3-none-any.whl
Size 974.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ea763fafd6f92c7e809752170c2bd6675a7a7d9f964e2763f46fef5af6810e2
BLAKE2b-256 checksum
How to use checksums
c38726c9c20edb1487e1170b82e441e90c2a6a67b826f3f6c69185f6711e2a88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.4 CPython/3.14.6 Linux/7.1.8-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

0.0.15

2 release files

0.0.14

2 release files

This release

0.0.12 This release

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