Skip to main content

Build Status - develop branch Coverage of the code

Provides a template loader that allows you to load a template from a specific application. This allows you to both extend and override a template at the same time.

The default Django loaders require you to copy the entire template you want to override, even if you only want to override one small block.

This is the issue that this package tries to resolve.

Examples:

You want to change the titles of the admin site, you would originally created this template:

$ cat my-project/templates/admin/base_site.html
{% extends "admin/base.html" %}
{% load i18n %}

{% block title %}{{ title }} | My Project{% endblock %}

{% block branding %}
<h1 id="site-name">My Project</h1>
{% endblock %}

{% block nav-global %}{% endblock %}

Extend and override version with a namespace:

$ cat my-project/templates/admin/base_site.html
{% extends "admin:admin/base_site.html" %}

{% block title %}{{ title }} - My Project{% endblock %}

{% block branding %}
<h1 id="site-name">My Project</h1>
{% endblock %}

Note that in this version the block nav-global does not have to be present because of the inheritance.

Shorter version without namespace:

$ cat my-project/templates/admin/base_site.html
{% extends ":admin/base_site.html" %}

{% block title %}{{ title }} - My Project{% endblock %}

{% block branding %}
<h1 id="site-name">My Project</h1>
{% endblock %}

If we do not specify the application namespace, the first matching template will be used. This is useful when several applications provide the same templates but with different features.

Example of multiple empty namespaces:

$ cat my-project/application/templates/application/template.html
{% block content%}
<p>Application</p>
{% endblock content%}

$ cat my-project/application_extension/templates/application/template.html
{% extends ":application/template.html" %}
{% block content%}
{{ block.super }}
<p>Application extension</p>
{% endblock content%}

$ cat my-project/templates/application/template.html
{% extends ":application/template.html" %}
{% block content%}
{{ block.super }}
<p>Application project</p>
{% endblock content%}

Will render:

<p>Application</p>
<p>Application extension</p>
<p>Application project</p>

Installation

First of all install django-app-namespace-template-loader with your favorite package manager. Example :

$ pip install django-app-namespace-template-loader

Once installed, add app_namespace.Loader to the TEMPLATE_LOADERS setting of your project.

TEMPLATE_LOADERS = [
  'app_namespace.Loader',
  ... # Other template loaders
]

With Django >= 1.8 app_namespace.Loader should be added to the 'loaders' section in the OPTIONS dict of the DjangoTemplates backend instead.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'loaders': [
                'app_namespace.Loader',
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
        },
    },
]

Note: With Django 1.8, app_namespace.Loader should be first in the list of loaders.

Known limitations

app_namespace.Loader can not work properly if you use it in conjunction with django.template.loaders.cached.Loader and inheritance based on empty namespaces.

Notes

Based originally on: http://djangosnippets.org/snippets/1376/

Requires: Django >= 1.8

Tested with Python 2.7, 3.3, 3.4.

If you want to use this application for previous versions of Django, use the version 0.3.1 of the package.

If you want to use this application with Python 2.6, use the version 0.2 of the package.

Metadata

Release files for django-app-namespace-template-loader 0.4.1

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

Source distribution (sdist)

Source distribution for django-app-namespace-template-loader 0.4.1
File Size Uploaded
django-app-namespace-template-loader-0.4.1.tar.gz 13.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-app-namespace-template-loader 0.4.1
File Interpreter ABI Platform
django_app_namespace_template_loader-0.4.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 29.0 kB

Release files / django-app-namespace-template-loader-0.4.1.tar.gz

Download URL django-app-namespace-template-loader-0.4.1.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7a450985479a2e07fe8a1e4e8208fc9e1d8b35503526dd28eba5f8ad4ba31d4e
BLAKE2b-256 checksum
How to use checksums
e65dc7ce27fbb1cf4fc14c6726d2c96d4072903678cbc047fd3bd524c7310658
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_app_namespace_template_loader-0.4.1-py2.py3-none-any.whl

Download URL django_app_namespace_template_loader-0.4.1-py2.py3-none-any.whl
Size 15.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
356539413b5d1de0eff91aea7a03806b5ef6874ee5420ea8c273f72bbc601d74
BLAKE2b-256 checksum
How to use checksums
2ea60a5ee33c8d0468b8e3aa8eca0c48dc1ecc5701adce63780b87cdf2df8e41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1

1 release file

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