Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Django Render Block ###################
.. image:: https://img.shields.io/pypi/v/django-render-block.svg :target: https://pypi.org/project/django-render-block/
.. image:: https://github.com/clokep/django-render-block/actions/workflows/main.yml/badge.svg :target: https://github.com/clokep/django-render-block/actions/workflows/main.yml
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.
Jinja2 <http://jinja.pocoo.org/>
__ engine: it does
not currently process the extends
tag.Django Render Block supports Django 4.2, 5.0, and 5.1 on Python 3.8, 3.9, 3.10, 3.11 and 3.12 (see the Django documentation for which versions of Python are supported by particular Django versions).
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'))
'block1 from test2'
>>> print(render_block_to_string('test2.html', 'block2'))
'block2 from test1'
It can also accept a context as a dict
(just like render_to_string
), in
test3.html
:
.. 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'}))
'Render this test!'
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_name``
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.
``block_name``
The name of the block to render from the above template.
``context``
A ``dict`` to be used as the template’s context for rendering. A ``Context``
object can be provided for Django templates.
``context`` is optional. If not provided, an empty context will be used.
``request``
The request object used to render the template.
``request`` is optional and works only for Django templates. If both context and request
are provided, a ``RequestContext`` will be used instead of a ``Context``.
Like render_to_string
this will raise the following exceptions:
``TemplateDoesNotExists``
Raised if the template(s) specified by ``template_name`` cannot be
loaded.
``TemplateSyntaxError``
Raised if the loaded template contains invalid syntax.
There are also two additional errors that can be raised:
``BlockNotFound``
Raised if the block given by ``block_name`` does not exist in the
template.
``UnsupportedEngine``
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,
please
file an issue <https://github.com/clokep/django-render-block/issues/new>
_ or
provide a pull request! Check the
list of issues <https://github.com/clokep/django-render-block/issues/>
_ for
ideas of what to work on.
This is based on a few sources:
Django Snippet 769 <https://djangosnippets.org/snippets/769/>
__Django Snippet 942 <https://djangosnippets.org/snippets/942/>
__Django-Block-Render <https://github.com/uniphil/Django-Block-Render/>
_django-templated-email <https://github.com/BradWhittington/django-templated-email/blob/master/templated_email/utils.py>
_StackOverflow answer 2687173 <http://stackoverflow.com/questions/2687173/django-how-can-i-get-a-block-from-a-template>
_.. :changelog:
Changelog #########
No changes from 0.10b1.
@yaakovLowenstein <https://github.com/yaakovLowenstein>
. (#52 <https://github.com/clokep/django-render-block/pull/52>
)#56 <https://github.com/clokep/django-render-block/pull/56>
_)#44 <https://github.com/clokep/django-render-block/pull/44>
,
#55 <https://github.com/clokep/django-render-block/pull/55>
)#44 <https://github.com/clokep/django-render-block/pull/44>
_)#44 <https://github.com/clokep/django-render-block/pull/44>
,
#55 <https://github.com/clokep/django-render-block/pull/55>
)#44 <https://github.com/clokep/django-render-block/pull/44>
,
#55 <https://github.com/clokep/django-render-block/pull/55>
)#54 <https://github.com/clokep/django-render-block/pull/54>
_)#36 <https://github.com/clokep/django-render-block/pull/36>
_)#37 <https://github.com/clokep/django-render-block/pull/37>
_)black <https://black.readthedocs.io/>
, isort <https://pycqa.github.io/isort/>
,
and flake8 <https://flake8.pycqa.org>
, and pyupgrade <https://github.com/asottile/pyupgrade>
.
(#38 <https://github.com/clokep/django-render-block/pull/38>
,
#39 <https://github.com/clokep/django-render-block/pull/39>
)Jack Linke <https://github.com/jacklinke>
.
(#41 <https://github.com/clokep/django-render-block/pull/41>
)#33 <https://github.com/clokep/django-render-block/pull/33>
_)@cordery <https://github.com/cordery>
. (#35 <https://github.com/clokep/django-render-block/pull/35>
)#31 <https://github.com/clokep/django-render-block/pull/31>
_)#27 <https://github.com/clokep/django-render-block/pull/27>
,
#31 <https://github.com/clokep/django-render-block/pull/31>
)#26 <https://github.com/clokep/django-render-block/pull/26>
,
#28 <https://github.com/clokep/django-render-block/pull/28>
)setup.cfg
.
(#32 <https://github.com/clokep/django-render-block/pull/32>
_)Context
could not be re-used. Contributed
by @evanbrumley <https://github.com/evanbrumley>
. (#25 <https://github.com/clokep/django-render-block/pull/25>
)render_block_to_string
now forwards the Context
passed as context
parameter.
Contributed by @bblanchon <https://github.com/bblanchon>
. (#21 <https://github.com/clokep/django-render-block/pull/21>
)#22 <https://github.com/clokep/django-render-block/pull/22>
_)#18 <https://github.com/clokep/django-render-block/pull/18>
_)#18 <https://github.com/clokep/django-render-block/pull/18>
,
#20 <https://github.com/clokep/django-render-block/pull/20>
)#19 <https://github.com/clokep/django-render-block/pull/19>
_)#18 <https://github.com/clokep/django-render-block/pull/18>
_)render_block_to_string
now optionally accepts a request
parameter.
If given, a RequestContext
instead of a Context
is used when
rendering with the Django templating engine. Contributed by
@vintage <https://github.com/vintage>
. (#15 <https://github.com/clokep/django-render-block/pull/15>
)#9 <https://github.com/clokep/django-render-block/pull/9>
,
#11 <https://github.com/clokep/django-render-block/pull/11>
,
#17 <https://github.com/clokep/django-render-block/pull/17>
_)#9 <https://github.com/clokep/django-render-block/pull/9>
,
#17 <https://github.com/clokep/django-render-block/pull/17>
)@mixxorz <https://github.com/mixxorz>
.
(#10 <https://github.com/clokep/django-render-block/pull/10>
)#8 <https://github.com/clokep/django-render-block/pull/8>
_)Jinja2 <http://jinja.pocoo.org/>
_ templating
engine. See README for caveats. (#3 <https://github.com/clokep/django-render-block/pull/3>
_)#5 <https://github.com/clokep/django-render-block/pull/5>
_)#6 <https://github.com/clokep/django-render-block/pull/6>
_)Largely rewritten.
Support Django 1.8 and 1.9:
context_instance
parameter.{{ block.super }}
.comment 3466 on Django Snippet 942 <https://djangosnippets.org/snippets/942/#c3466>
_
by eugenyboger <https://djangosnippets.org/users/eugenyboger/>
_
to fix an issue with nested extends. The specific bug was not reproducible,
but the additional code shouldn't hurt.comment 3237 on Django Snippet 942 <https://djangosnippets.org/snippets/942/#c3237>
_
by chadselph <https://djangosnippets.org/users/chadselph/>
_
to remove a pointless render. The specific bug was not reproducible, but the
removed code was extraneous.Updated version from
Django Snippet 942 <https://djangosnippets.org/snippets/942/>
_ by
zbyte64 <https://djangosnippets.org/users/zbyte64/>
_.
Improves include:
render_to_response
Django Snippet 769 <https://djangosnippets.org/snippets/769/>
_ by
sciyoshi <https://djangosnippets.org/users/sciyoshi/>
_.FAQs
Render a particular block from a template to a string.
We found that django-render-block demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.