Skip to content

Commit 38ab10d

Browse files
committed
Fix AttributeError in base.html when called without response_headers
This happens when using a `404.html` which extends DRF's `base.html` and is called by a `Resolver404`. Since `Resolver404` goes via django's `handler404` and not via DRF's exception handling, it doesn't get the extra context. Since the `|items` filter doesn't check for `response_headers` being null (missing) it attempts to call `<None>.items()`, thus raising an AttributeError. Alternatives: * This could alternatively be solved in the `items` filter, making it just check for null before continuing. * If there was an easy way to delegate `handler404` etc to a DRF-based implementation which added the correct context, that might be an ideal solution. I couldn't find an easy way to do this.
1 parent c17b4ad commit 38ab10d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

rest_framework/templates/rest_framework/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ <h1>{{ name }}</h1>
157157
</div>
158158

159159
<div class="response-info" aria-label="{% trans "response info" %}">
160-
<pre class="prettyprint"><span class="meta nocode"><b>HTTP {{ response.status_code }} {{ response.status_text }}</b>{% autoescape off %}{% for key, val in response_headers|items %}
161-
<b>{{ key }}:</b> <span class="lit">{{ val|break_long_headers|urlize_quoted_links }}</span>{% endfor %}
160+
<pre class="prettyprint"><span class="meta nocode"><b>HTTP {{ response.status_code }} {{ response.status_text }}</b>{% autoescape off %}{% if response_headers %}{% for key, val in response_headers|items %}
161+
<b>{{ key }}:</b> <span class="lit">{{ val|break_long_headers|urlize_quoted_links }}</span>{% endfor %}{% endif %}
162162

163163
</span>{{ content|urlize_quoted_links }}</pre>{% endautoescape %}
164164
</div>

tests/test_templates.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.shortcuts import render
2+
3+
4+
def test_base_template_with_no_context():
5+
# base.html should be renderable with no context,
6+
# so it can be easily extended.
7+
render({}, 'rest_framework/base.html')

0 commit comments

Comments
 (0)