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 3 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
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
10 changes: 7 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,13 @@
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_instance.push(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 %}
18 changes: 16 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from unittest import skip

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

from render_block import render_block_to_string, BlockNotFound, UnsupportedEngine
Expand Down Expand Up @@ -99,6 +98,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