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

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.6
File Size Uploaded
dj_templates-0.0.6.tar.gz 951.3 kB Details

Built distribution (wheel)

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

Total release size: 1.9 MB

Release files / dj_templates-0.0.6.tar.gz

Download URL dj_templates-0.0.6.tar.gz
Size 951.3 kB
Tags Source
SHA-256 checksum
How to use checksums
3f79ba4f321e104f4caa53e62671c351d6c4685a0cd7a55174f42c53a5ba0fa8
BLAKE2b-256 checksum
How to use checksums
69e7902b0c7bdd78a1b885a328229d571b6eb4627557942edf337c8a6eb75685
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.4 CPython/3.14.6 Linux/7.1.6-1-cachyos

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

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

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

This release

0.0.6 This release

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