/
Depends on
- dh-python (dh-python)
- jinja2 (python3-jinja2)
- python-django (python3-django)
- python3-defaults (python3-all)
- setuptools (python3-setuptools)
- debhelper-compat
Used by
README (full)
Django Render Block
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.
Features
Render a specific block from a template
Fully supports the Django templating engine
-
Partially supports the Jinja2_ engine: it does
not currently process the
extendstag.
Requirements
Django Render Block supports Django 2.2, 3.0, and 3.1 on Python 3.6, 3.7, 3.8, and 3.9.
Examples
In test1.html:
{% block block1 %}block1 from test1{% endblock %}
{% block block2 %}block2 from test1{% endblock %}
In test2.html:
{% extends 'test1.html' %}
{% block block1 %}block1 from test2{% endblock %}
And from the Python shell:
>>> 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 test3.html:
{% block block3 %}Render this {{ variable }}!{% endblock %}
And from 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, request=None)
template_nameThe name of the template to load and render. If it’s a list of template names, Django usesselect_template()instead ofget_template()to find the template.
block_nameThe name of the block to render from the above template.
contextAdictto be used as the template’s context for rendering. AContextobject can be provided for Django templates.
contextis optional. If not provided, an empty context will be used.
requestThe request object used to render the template.
requestis optional and works only for Django templates. If both context and request are provided, aRequestContextwill be used instead of aContext.
Exceptions
Like render_to_string this will raise the following exceptions:
TemplateDoesNotExistsRaised if the template(s) specified bytemplate_namecannot be loaded.
TemplateSyntaxErrorRaised if the loaded template contains invalid syntax.
There are also two additional errors that can be raised:
BlockNotFoundRaised if the block given byblock_namedoes not exist in the template.
UnsupportedEngineRaised if a template backend besides the Django backend is used.
Contributing
If you find a bug or have an idea for an improvement to Django Render Block, please file an issue or provide a pull request! Check the list of issues for ideas of what to work on.
Attribution
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
| dir | debian/ |
| dir | django_render_block.egg-info/ |
| dir | render_block/ |
| dir | tests/ |
| file | CHANGELOG.rst |
| file | LICENSE |
| file | MANIFEST.in |
| file | PKG-INFO |
| file | README.rst |
| file | setup.cfg |
| file | setup.py |