Work in progress (WIP), ticket #28935, Template error raised in an {% extends %} child template shows incorrect source location on debug page

95 views
Skip to first unread message

Min ho Kim

unread,
Sep 12, 2019, 1:18:42 PM9/12/19
to Django developers (Contributions to Django itself)
Hi,
I am trying to contribute to code.

I picked my first issue to solve.
https://code.djangoproject.com/ticket/28935
And wanted to solve for the last few weeks, and finally kinda fixed it, but I don't know whether it's good enough.
Feels like it's just monkey patched of something.
Please have a look.

To summarise the issue #28935:
When "extends" and "include" template tags are used together in same html page, if "include" tries to include nonexistent html file,
error page gives incorrect information (filename and line number) under the heading "Error during template rendering".

For example,

[base.html]
{% block content %}
{% endblock %}


[home.html]

{% extends 'base.html' %}
{% block content %}
{% include "./nonexistent.html" %}
{% endblock %}

This gives

Error during template rendering
In template /.../base.html, error at line 0
nonexistent.html

But the expected error should be

In template /.../home.html, error at line 3
nonexistent.html

So here's my code change:

[django > template > context.py ]
# made a new global variable at the top in context.py
template_stack = []

[django > template > base.py > Node > render_annotated]
def render_annotated(self, context):
try:
return self.render(context)
except Exception as e:
if context.template.engine.debug and not hasattr(e, 'template_debug'):
from django.template.context import template_stack

# template_stack[-1].name ==> "base.html"
# template_stack[-2].nodelist[0].token.contents ==> "extends 'base.html"
try:
if len(template_stack) >= 2 and "extends" in template_stack[-2].nodelist[0].token.contents and template_stack[-1].name in template_stack[-2].nodelist[0].token.contents:
e.template_debug = context.template.get_exception_info(e, self.token)
else:
e.template_debug = context.render_context.template.get_exception_info(e, self.token)
except (AttributeError, IndexError, KeyError):
e.template_debug = context.render_context.template.get_exception_info(e, self.token)
raise



First of all, there is 1 test failure.
But if I manually test the failed one by making exact same files being tested, error page seems to be Okay.

Second, is this ways of code change acceptable?

======================================================================
FAIL: test_compile_tag_error_27956 (template_tests.tests.TemplateTests)
Errors in a child of {% extends %} are displayed correctly.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 628, in run
testMethod()
File "/Users/minhokim/Code/Repo/django/tests/template_tests/tests.py", line 135, in test_compile_tag_error_27956
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 852, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 845, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 'nt.html" %}\n' != '{% badtag %}'

----------------------------------------------------------------------

Thank you.

- Min ho Kim

Carlton Gibson

unread,
Sep 12, 2019, 1:22:25 PM9/12/19
to Django developers (Contributions to Django itself)
Hello! Super. 🙂

Can I ask you to put it in a PR on GitHub, so it’s easier to test and review?

Thanks.

Kind Regards,

Carlton
> --
> You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/e4b1c45a-8750-4ae4-8985-dd09421d06a8%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages