Skip to main content

Django Template Data

Why?

You are working on a Django project and you want a way to update some strings or blocks without using a bare CMS or a WYSIWYG. Then this small module is for you. It does what you expect, load datas from database and send them via the context to the templates.

Installation

$ pip install git+https://github.com/jefcolbi/django-template-data.git

Usage

Add 'template_data' in INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    'template_data',
    ...,
]

Then add load_data() to templates context processors

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug': True,
            'context_processors': [
                ...,
                'template_data.processors.load_data',
            ],
        },
    },
]

Finally migrate

$ python manage.py migrate

Tutorial

Let say you want a dynamic title, loaded from the database. First create a base template like this:

{% load i18n %}
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="shortcut icon" href="/static/front/img/favicon.png" type="image/x-icon">
    {% block full_title %}<title>{% block title %}{{ title }}{% endblock %} - Sitename</title>{% endblock %}

...

Go to your Django admin to manage TemplateData model. Create two rows, with these values:
key = "title", page = "index", value = "Home"
key = "title", page = "signin", value = "Login in your account"
As you can imagine the title of the index page will be "Home - Sitename" and for the signin page will be "Login in your account - Sitename"

You surely noticed how we appended "- Sitename" to the title in the template. We can do the same by using the inheriting feature.
First we define this template:

{% load i18n %}
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="shortcut icon" href="/static/front/img/favicon.png" type="image/x-icon">
    <title>{% block title %}{{ title }}{% endblock %}</title>

...

Then create three rows of TemplateData with these values:
key = "title", page = "global", value = "Sitename"
key = "title", page = "index", inheriting_page = "global", value = "Home - {{ super }}"
key = "title", page = "signin", inheriting_page = "global", value = "Login in your account - {{ super }}"
You will have the same result.

Internalization

You can also add the lang attribute for multilingualism, but for this to work you need to set the LANGUAGE variables in your project settings.

{% load i18n %}
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="shortcut icon" href="/static/front/img/favicon.png" type="image/x-icon">
    {% block full_title %}<title>{% block title %}{{ title }}{% endblock %} - Sitename</title>{% endblock %}

...

Go to your Django admin to manage TemplateData model. Create two rows, with these values:
key = "title", page = "signin", lang = english, value = "Login in your account"

key = "title", page = "signin", lang = french, value = "Connexion a votre compte"

As you can imagine the title of the signin page will be "Login in your account - Sitename" if browser language is in english and "Connexion a votre compte - Sitename" if french

Restore data

After saving in bd all the texts, use the commandpython manage.py save_data file_name.json to save all that in a json file.

To restore these data in your database use python manage.py restore_data file_name.json

Template rewriting

Edit

Template rewriting is a feature provided to re-write existing templates to conform with Django Template Data.

Start by editing your templates and add the attribute tpl-data-key in the tags you want to create template data for.
For example, if you have a h2 tag like this
<h2>My custom title</h2>

you can edit it like this
<h2 tpl-data-key="title">My custom title</h2>

It will be rewritten in
<h2>{{ title }}</h2>

and a new template data entry will be added in database.

You can inform about the tag's text language by using tpl-data-lang like
<h2 tpl-data-key="title" tpl-data-lang="en">My custom title</h2>
to inform that your text is in English.

Process

After you have edited your templates files run the django command rewrite_template
Examples:

To rewrite all the templates linked to your views, do:
python manage.py rewrite_template --views

To rewrite a single base template file, do:
python manage.py rewrite_template --file=core/base.html

To rewrite a single template file, do:
python manage.py rewrite_template --file=core/index.html --pagename=index

Contributing

Contributions are welcome. It is FOSS!

License

Feel free to use it as you want.

Metadata

Release files for django-template-data 0.1.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-template-data 0.1.1
File Size Uploaded
django-template-data-0.1.1.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-template-data 0.1.1
File Interpreter ABI Platform
django_template_data-0.1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 25.6 kB

Release files / django-template-data-0.1.1.tar.gz

Download URL django-template-data-0.1.1.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
28e3821a1fbdc6c9392d6298fe5805ddaaf8bf75301eb637f96ab25d81f0ef5c
BLAKE2b-256 checksum
How to use checksums
12248d4c85d7e5e55a18877743b6df573417937dec90e6bf906a5844c40b1e9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release files / django_template_data-0.1.1-py2.py3-none-any.whl

Download URL django_template_data-0.1.1-py2.py3-none-any.whl
Size 14.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
16eff4ade57734afbac2e5b5c186d8e95dfb7ff1d506334c986b95efb5dfd8c2
BLAKE2b-256 checksum
How to use checksums
98425900a7d98e942c70a9a0c8ddd4435e9d1827aab51a5b7604a4341b9000d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.0.7

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