Skip to content

Commit

Permalink
Merge pull request #21 from bblanchon/forward-context-instance
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Oct 2, 2020
2 parents e0aef62 + e68bd9f commit 6f243f0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
Changelog
#########

next (xxx xx, xxxx)
===================

* ``render_block_to_string`` now forwards the ``Context`` passed as ``context`` parameter.
(`#21 <https://github.com/clokep/django-render-block/pull/21>`_, by @bblanchon)

0.7 (July 13, 2020)
===================

Expand All @@ -17,7 +23,7 @@ Changelog
* Supports Django 1.11, Django 2.1, and Django 2.2.
* Supports Python 2.7, 3.5, 3.6, and 3.7.
* ``render_block_to_string`` now optionally accepts a ``request`` parameter.
If given a ``RequestContext`` instead of a ``Context`` is used when
If given, a ``RequestContext`` instead of a ``Context`` is used when
rendering with the Django templating engine. See
`#15 <https://github.com/clokep/django-render-block/pull/15>`_, thanks to
@vintage.
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ The API is simple and attempts to mirror the built-in ``render_to_string`` API.
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 ``dict`` or a ``Context`` to be used as the template’s context for rendering.

``context`` is now optional. An empty context will be used if it isn’t
provided.
Expand All @@ -88,7 +88,7 @@ The API is simple and attempts to mirror the built-in ``render_to_string`` API.
The request object used to render the template.

``request`` is optional and works only for Django templates. If
provided a ``RequestContext`` will be used instead of a ``Context``.
provided, a ``RequestContext`` will be used instead of a ``Context``.

Exceptions
----------
Expand Down
6 changes: 4 additions & 2 deletions render_block/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@


def django_render_block(template, block_name, context, request=None):
# Create a Django Context.
if request:
# Create a Django Context if needed
if isinstance(context, Context):
context_instance = context
elif request:
context_instance = RequestContext(request, context)
else:
context_instance = Context(context)
Expand Down
7 changes: 7 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import skip

from django.template import Context
from django.test import modify_settings, override_settings, TestCase, RequestFactory

from render_block import render_block_to_string, BlockNotFound, UnsupportedEngine
Expand Down Expand Up @@ -79,6 +80,12 @@ def test_context(self):
result = render_block_to_string('test5.html', 'block2', {'foo': data})
self.assertEqual(result, data)

def test_context_autoescape_off(self):
"""Test that the user can disable autoescape by providing a Context instance."""
data = "&'"
result = render_block_to_string('test5.html', 'block2', Context({'foo': data}, autoescape=False))
self.assertEqual(result, data)

@override_settings(
TEMPLATES=[{
'BACKEND': 'django.template.backends.dummy.TemplateStrings',
Expand Down

0 comments on commit 6f243f0

Please sign in to comment.