Skip to main content

Render a particular block from a template to a string.

Project description

Django Render Block

.. image::

Render the content of a specific block tag from a Django template. Works for
arbitrary template inheritance, even if a block is defined in the child template
but not in the parent. Generally it works like ``render_to_string`` from Django,
but allows you to specify a block to render.


Django Render Block supports Django 1.8 and 1.9. You must use the Django
template rendering engine (the default) to render specific blocks.


In ``test1.html``:

.. code-block:: jinja

{% block block1 %}block1 from test1{% endblock %}
{% block block2 %}block2 from test1{% endblock %}

In ``test2.html``:

.. code-block:: jinja

{% extends 'test1.html' %}
{% block block1 %}block1 from test2{% endblock %}

And from the Python shell:

.. code-block:: python

>>> from render_block import render_block_to_string
>>> print render_block_to_string('test2.html', 'block1')
u'block1 from test2'
>>> print render_block_to_string('test2.html', 'block2')
u'block2 from test1'

It can also accept a context as a ``dict`` (just like ``render_to_string``), in

.. code-block:: jinja

{% block block3 %}Render this {{ variable }}!{% endblock %}

And from Python:

.. code-block:: python

>>> print render_block_to_string('test3.html', 'block3', {'variable': 'test'})
u'Render this test!'

API Reference

The API is simple and attempts to mirror the built-in ``render_to_string`` API.

``render_block_to_string(template_name, block_name, context=None)``

The name of the template to load and render. If it’s a list of template
names, Django uses ``select_template()`` instead of ``get_template()``
to find the template.

The name of the block to render from the above template.

A ``dict`` to be used as the template’s context for rendering.

``context`` is now optional. An empty context will be used if it isn’t


Like ``render_to_string`` this will raise the following exceptions:

Raised if the template(s) specified by ``template_name`` cannot be

Raised if the loaded template contains invalid syntax.

There are also two additional errors that can be raised:

Raised if the block given by ``block_name`` does not exist in the

Raised if a template backend besides the Django backend is used.


If you find a bug or have an idea for an improvement to Django Render Block,
`file an issue <>`_ or
provide a pull request! Check the
`list of issues <>`_ for
ideas of what to work on.


This is based on a few sources:

* Originally `Django Snippet 769 <>`_
* Updated version `Django Snippet 942 <>`_
* A version of the snippets was ported as `Django-Block-Render <>`_
* Additionally inspired by part of `django-templated-email <>`_
* Also based on a `StackOverflow answer 2687173 <>`_
.. :changelog:


0.3.1 (June 1, 2016)

* Refactoring to make more generic (for potentially supporting multiple
templating engines).

0.3 (May 27, 2016)

* Largely rewritten.
* Updated to support modern Django (1.8, 1.9):

* Guards against different template backends.
* Uses internal APIs for each node.
* Removed ``context_instance`` parameter.
* Support for calling ``{{ block.super }}``.

0.2.2 (January 10, 2011)

* Updated per
`comment 3466 on Django Snippet 942 <>`_
to fix an issue with nested extends. The specific bug was not reproducible,
but the additional code shouldn't hurt.

0.2.1 (August 27, 2010)

* Updated per
`comment 3237 on Django Snippet 942 <>`_
to remove a pointless render. The specific bug was not reproducible, but the
code is extraneous.

0.2 (August 4, 2008)

* Updated version from
`Django Snippet 942 <>`_ by zbyte64.
* Improves include:

1. Simpler/better handling of "extends" block tag
2. Searches If/Else blocks
3. Less code
4. Allow list of templates to be passed which is closer to the behavior of

0.1 (May 22, 2008)

* Initial version from
`Django Snippet 769 <>`_ by sciyoshi.
* Supports Django 0.96.

Project details

Download files

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

Files for django-render-block, version 0.3.1
Filename, size File type Python version Upload date Hashes
Filename, size django_render_block-0.3.1-py2.py3-none-any.whl (17.4 kB) File type Wheel Python version py2.py3 Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page