Skip to content

Commit 9629886

Browse files
craigdscarltongibson
authored andcommitted
Fixed AttributeError from items filter when value is None (#5981)
1 parent c17b4ad commit 9629886

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

rest_framework/templatetags/rest_framework.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ def items(value):
240240
lookup. See issue #4931
241241
Also see: https://stackoverflow.com/questions/15416662/django-template-loop-over-dictionary-items-with-items-as-key
242242
"""
243+
if value is None:
244+
# `{% for k, v in value.items %}` doesn't raise when value is None or
245+
# not in the context, so neither should `{% for k, v in value|items %}`
246+
return []
243247
return value.items()
244248

245249

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)