Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for RequestContext during block rendering #15

Merged
merged 9 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ 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)``
``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
Expand All @@ -84,6 +84,13 @@ The API is simple and attempts to mirror the built-in ``render_to_string`` API.
``context`` is now optional. An empty context will be used if it isn’t
provided.

``request``
The request object used to render the template.

``request`` is optional and works only for Django templates.
Standard ``Context` instead of ``RequestContext`` will be used if
it isn’t provided.

Exceptions
----------

Expand Down
4 changes: 2 additions & 2 deletions render_block/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Jinja2Template(object):
from render_block.exceptions import BlockNotFound, UnsupportedEngine


def render_block_to_string(template_name, block_name, context=None):
def render_block_to_string(template_name, block_name, context=None, request=None):
"""
Loads the given template_name and renders the given block with the given
dictionary as context. Returns a string.
Expand All @@ -36,7 +36,7 @@ def render_block_to_string(template_name, block_name, context=None):

# The Django backend.
if isinstance(t, DjangoTemplate):
return django_render_block(t, block_name, context)
return django_render_block(t, block_name, context, request)

elif isinstance(t, Jinja2Template):
from render_block.jinja2 import jinja2_render_block
Expand Down
9 changes: 6 additions & 3 deletions render_block/django.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import

from django.template import Context
from django.template.base import TextNode
from django.template.context import Context, RequestContext
from django.template.loader_tags import (BLOCK_CONTEXT_KEY,
BlockContext,
BlockNode,
Expand All @@ -10,9 +10,12 @@
from render_block.exceptions import BlockNotFound


def django_render_block(template, block_name, context):
def django_render_block(template, block_name, context, request=None):
# Create a Django Context.
context_instance = Context(context)
if request:
context_instance = RequestContext(request, context)
else:
context_instance = Context(context)

# Get the underlying django.template.base.Template object.
template = template.template
Expand Down
3 changes: 3 additions & 0 deletions tests/templates/test_request_context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% include 'test1.html' %}

{% block block1 %}{{ request.path }}{% endblock %}
17 changes: 16 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import skip

from django.test import override_settings, TestCase
from django.test import modify_settings, override_settings, TestCase, RequestFactory
try:
from django.utils import six
PY2 = six.PY2
Expand Down Expand Up @@ -103,6 +103,21 @@ def test_different_backend(self):
self.assertExceptionMessageEquals(exc.exception,
"Can only render blocks from the Django template backend.")

@modify_settings(
INSTALLED_APPS={
'prepend': [
'django.contrib.auth',
'django.contrib.contenttypes',
],
},
)
def test_request_context(self):
"""Test that a request context data are properly rendered in a template."""
request = RequestFactory().get('dummy-url')
result = render_block_to_string('test_request_context.html', 'block1', {}, request)

self.assertEqual(result, u'/dummy-url')


@override_settings(
TEMPLATES=[{
Expand Down