Skip to content

Commit

Permalink
Fix errors in templates propagting when DEBUG is enabled. (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaakovLowenstein authored Jun 28, 2024
1 parent b2d4b1a commit 4ccec65
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ pip-log.txt

# Editor files
.idea

# Virtual environment
.venv
37 changes: 20 additions & 17 deletions render_block/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,29 @@ def django_render_block(template, block_name, context, request=None):
else:
context_instance = Context(context)

# Get the underlying django.template.base.Template object.
# Get the underlying django.template.base.Template object from the backend.
template = template.template

# Bind the template to the context.
with context_instance.bind_template(template):
# Before trying to render the template, we need to traverse the tree of
# parent templates and find all blocks in them.
parent_template = _build_block_context(template, context_instance)

try:
return _render_template_block(template, block_name, context_instance)
except BlockNotFound:
# The block wasn't found in the current template.

# If there's no parent template (i.e. no ExtendsNode), re-raise.
if not parent_template:
raise

# Check the parent template for this block.
return _render_template_block(parent_template, block_name, context_instance)
with context_instance.render_context.push_state(template):
with context_instance.bind_template(template):
# Before trying to render the template, we need to traverse the tree of
# parent templates and find all blocks in them.
parent_template = _build_block_context(template, context_instance)

try:
return _render_template_block(template, block_name, context_instance)
except BlockNotFound:
# The block wasn't found in the current template.

# If there's no parent template (i.e. no ExtendsNode), re-raise.
if not parent_template:
raise

# Check the parent template for this block.
return _render_template_block(
parent_template, block_name, context_instance
)


def _build_block_context(template, context):
Expand Down
5 changes: 5 additions & 0 deletions tests/templates/test_exception.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load test_tags %}

{% block exception_block %}
{% raise_exception %}
{% endblock exception_block %}
8 changes: 8 additions & 0 deletions tests/templatetags/test_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django import template

register = template.Library()


@register.simple_tag()
def raise_exception():
raise Exception("Exception raised in template tag.")
13 changes: 13 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ def test_subblock_no_parent(self):
result = render_block_to_string("test_sub.html", "first")
self.assertEqual(result, "\nbar\n")

def test_exceptions(self):
with self.assertRaises(Exception) as e:
render_block_to_string("test_exception.html", "exception_block")
self.assertEqual(str(e.exception), "Exception raised in template tag.")

@override_settings(DEBUG=True)
def test_exceptions_debug(self):
with self.assertRaises(Exception) as exc:
render_block_to_string("test_exception.html", "exception_block")
self.assertExceptionMessageEquals(
exc.exception, "Exception raised in template tag."
)

def test_context(self):
"""Test that a context is properly rendered in a template."""
data = "block2 from test5"
Expand Down

0 comments on commit 4ccec65

Please sign in to comment.