niwinz / django-jinja

Simple and nonobstructive jinja2 integration with Django.
http://niwinz.github.io/django-jinja/latest/
BSD 3-Clause "New" or "Revised" License
363 stars 102 forks source link

Error with template errors with Django and debug #238

Closed tomhamiltonstubber closed 4 years ago

tomhamiltonstubber commented 4 years ago

This error happens for Authenticated users, where there's an error in rendering the template.

If I uninstall django-jinja and use jinja templates the way Django recommends in their docs, then I have no issues.

Expected Behaviour

Error renders properly in browser with stacktrace

Actual Behaviour

A server error occurred. Please contact the administrator. is shown in the browser, and a long stacktrace in the console.

Template Code

I created a repo with an easily replicated example:

https://github.com/tomhamiltonstubber/jinja2-error-example

Full Traceback


# The first error I'm deliberately causing

  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/tom/repos/jinja-error-replication/JinjaError/main/views.py", line 9, in index
    return render(request, 'index.jinja', {'foo': 'Bar'})
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django_jinja/backend.py", line 106, in render
    return mark_safe(self.template.render(context))
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "templates/index.jinja", line 1, in top-level template code
    {% extends 'base.jinja' %}
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/jinja2/environment.py", line 1005, in render
    return concat(self.root_render_func(self.new_context(vars)))
  File "templates/index.jinja", line 14, in root
  File "templates/base.jinja", line 11, in root
  File "templates/index.jinja", line 23, in block_content
ValueError: not enough values to unpack (expected 2, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/utils/deprecation.py", line 94, in __call__
    response = response or self.get_response(request)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/views/debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/views/debug.py", line 333, in get_traceback_html
    c = Context(self.get_traceback_data(), use_l10n=False)
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/views/debug.py", line 264, in get_traceback_data
    frames = self.get_traceback_frames()
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/views/debug.py", line 422, in get_traceback_frames
    filename, lineno, 7, loader, module_name,
  File "/home/tom/repos/jinja-error-replication/env/lib/python3.7/site-packages/django/views/debug.py", line 383, in _get_lines_from_file
    context_line = source[lineno]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

And then the same thing * 7.

Your Environment

LowerDeez commented 4 years ago

I also have this issue with Jinja2==2.10.3. With 2.10.1 everyting is ok.

akx commented 4 years ago

Seeing this too. This is mitigated by https://github.com/django/django/pull/11886 (and you can apply the same try: except IndexError: patch yourself as a workaround).

akx commented 4 years ago

This is apparently caused by https://github.com/pallets/jinja/commit/3a86ef3d72fdefb149c0c859e6e37035826f3557 – for some reason the "ugly crap" works, the replaced tb_next doesn't. I'll look into if it's something we can fix on django-jinja's side here...

More precisely, with the tb_next patch, it looks like exception line numbers are wrong ("line 104 out of 67").

akx commented 4 years ago

Yeah, since I can't really see any code that would mangle render() tracebacks in django-jinja, this smells like a Jinja2 regression. Going to report over there :)

wizpig64 commented 4 years ago

It looks like the bug is closed on Jinja's side, and they've released since then as well.

Is it safe to close this issue?