Skip to content

Fix IndexError: list index out of range #1156

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

Merged
merged 2 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion debug_toolbar/panels/templates/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,16 @@ def _store_template_info(self, sender, **kwargs):
temp_layer[key] = value
finally:
recording(True)
# Execute pformat first - if for some reason pformat/repr
# causes more templates to be rendered, seen/pformat layers
# will still be consistent
pformatted = pformat(temp_layer)
# Refs GitHub issue #910
# If we've not seen the layer before then we will add it
# so that if we see it again we can skip formatting it.
self.seen_layers.append(key_values)
# Note: this *ought* to be len(...) - 1 but let's be safe.
index = self.seen_layers.index(key_values)
pformatted = pformat(temp_layer)
# Note: this *ought* to be len(...) - 1 but let's be safe.
self.pformat_layers.insert(index, pformatted)
context_list.append(pformatted)
Expand Down
9 changes: 9 additions & 0 deletions tests/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import forms
from django.contrib.auth.models import User


class TemplateReprForm(forms.Form):
user = forms.ModelChoiceField(queryset=User.objects.all())

def __repr__(self):
return str(self)
13 changes: 13 additions & 0 deletions tests/panels/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.test import TestCase, override_settings

from ..base import BaseTestCase
from ..forms import TemplateReprForm
from ..models import NonAsciiRepr


Expand Down Expand Up @@ -35,6 +36,18 @@ def test_queryset_hook(self):
self.assertIn("<<queryset of auth.User>>", ctx)
self.assertIn("<<triggers database query>>", ctx)

def test_template_repr(self):
# Force widget templates to be included
self.toolbar.config["SKIP_TEMPLATE_PREFIXES"] = ()

User.objects.create(username="admin")
bad_repr = TemplateReprForm()
t = Template("{{ bad_repr }}")
c = Context({"bad_repr": bad_repr})
html = t.render(c)
self.assertIsNotNone(html)
self.assertValidHTML(html)

def test_object_with_non_ascii_repr_in_context(self):
response = self.panel.process_request(self.request)
t = Template("{{ object }}")
Expand Down