Skip to main content

Template loader allowing you to both extend and override a template at the same time.

Project description

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
]

Or if you use Django >= 1.8 add app_namespace.Loader to the 'loaders' section in the OPTION of the DjangoTemplates backend.

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

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

Tested with Python 2.7, 3.2, 3.3, 3.4.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

django_app_namespace_template_loader-0.3-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-app-namespace-template-loader-0.3.tar.gz.

File metadata

File hashes

Hashes for django-app-namespace-template-loader-0.3.tar.gz
Algorithm Hash digest
SHA256 07eafb23b6f159e5a294aa275794130f88626c75df4dedccbbf4522596557cca
MD5 a290d20dcda961eab117519d9f741c3a
BLAKE2b-256 9e1c9402ff0cc27557f35b7bea9d6996a8fba165bb2be439bad888886d936cf8

See more details on using hashes here.

File details

Details for the file django_app_namespace_template_loader-0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_app_namespace_template_loader-0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 57cfbd8d2c27cde66f602df6f1f997b013b6faf20c79824584c9609771ab7c59
MD5 c90222a1585775dfddc5c7813439f8f5
BLAKE2b-256 02ca9856dbdea4488cc6467db572cf566068d82ad27fe46c29ef0ad409c24003

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page