django-static-templates

Latest Version CI Status Coverage Status Documentation Status

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.

configuration = None[source]
stdout = None[source]
log(message)[source]
render()[source]

render starts the rendering process for the given configuration.

get_template_name()[source]

Returns the template name to use when rendering to static.

get_template_context()[source]

Provides the context for render_template call.

render_template()[source]

Creates a fake request context together with the context from get_template_context and returns the rendered output.

get_static_file_path()[source]

Returns the target static file path to store the content to.

write_static_file(content)[source]

write_static_file makes sure that the target directory exists and writes the provided content to a file.

static_templates.renderer.get_renderer(path=None)[source]

Load a renderer and return the class. If a path is provided, the renderer is imported from that path. By default, static_templates.renderer.Renderer is used.

Indices and tables