django-static-templates¶
django-static-templates provides a management command to render Django templates 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 interpreter is required in addition to poetry.
$ poetry install
Now you’re ready to run the tests:
$ make tests
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
Changelog¶
0.0.3 (2021-02-05)¶
Ensure to use a real SessionStore when faking the request
0.0.2 (2020-04-16)¶
Pass request to render_to_string
0.0.1 (2018-08-08)¶
Initial release of django-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.