django-static-templates¶
django-static-templates provides a management command to render Django templats as static files. Example usage might be static error pages to delivered by your reverse proxy if the Django applications dies.
Features¶
- Management command
render_static_templates
to create static files in your STATIC_ROOT. - Helpers to render templates as static files
Requirements¶
django-static-templates supports Python 3 only and requires at least Django 1.11.
Prepare for development¶
A Python 3.6 interpreter is required in addition to pipenv.
$ pipenv install --python 3.6 --dev
$ pipenv shell
$ pip install -e .
Now you’re ready to run the tests:
$ pipenv run py.test
Resources¶
Contents:
Installation¶
Install with pip:
pip install django-static-templates
Your
INSTALLED_APPS
setting:INSTALLED_APPS = ( # ... 'static_templates', )
Usage¶
Configure templates to render¶
To configure the templates that should be rendered as static files, add the
STATIC_TEMPLATES
setting to your configuration.
STATIC_TEMPLATES = (
('some-template.html', 'rendered.html'),
('500.html', 'errors/500.html'),
)
The setting should be a iterable that returns tuples with to elements.
The tuple’s first element is the template path, the second is the path inside
settings.STATIC_ROOT
where the rendered content should be stored.
Providing extra context to the templates¶
Remember that the templates are rendered using Django’s render_to_string
.
If you need extra context when rendering the templates, configure
STATIC_TEMPLATES_CONTEXT
as a dictionary.
STATIC_TEMPLATES_CONTEXT = {
'DEBUG': False,
'RAVEN_DSN': 'Some token'
}
Using a different renderer¶
If you want to change the way the templates are rendered, you can override the
used renderer by configuring the STATIC_TEMPLATES_RENDERER
setting.
By default, static_templates.renderer.Renderer
is used. Feel free to sub-class
and extend the functionality.
Rendering the templates¶
To render the templates, use the management command render_static_templates
.
$ python manage.py render_static_templates
Api documentation:
static_templates.renderer¶
-
class
static_templates.renderer.
Renderer
(configuration, stdout=None)[source]¶ Bases:
object
Renderer is used to convert a template into a static file.
-
__init__
(configuration, stdout=None)[source]¶ The init function takes a row from settings.STATIC_TEMPLATES. By default, the users are required to provide a two-item tuple with the template name and the static file path.
-