From 9629886915b7f3325e3dcb586e5eaed0cc395861 Mon Sep 17 00:00:00 2001 From: Craig de Stigter Date: Fri, 11 May 2018 18:50:08 +1200 Subject: [PATCH] Fixed AttributeError from items filter when value is None (#5981) --- rest_framework/templatetags/rest_framework.py | 4 ++++ tests/test_templates.py | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/test_templates.py diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index 2a2459b37..36aa9e8b3 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -240,6 +240,10 @@ def items(value): lookup. See issue #4931 Also see: https://stackoverflow.com/questions/15416662/django-template-loop-over-dictionary-items-with-items-as-key """ + if value is None: + # `{% for k, v in value.items %}` doesn't raise when value is None or + # not in the context, so neither should `{% for k, v in value|items %}` + return [] return value.items() diff --git a/tests/test_templates.py b/tests/test_templates.py new file mode 100644 index 000000000..a296395f6 --- /dev/null +++ b/tests/test_templates.py @@ -0,0 +1,7 @@ +from django.shortcuts import render + + +def test_base_template_with_no_context(): + # base.html should be renderable with no context, + # so it can be easily extended. + render({}, 'rest_framework/base.html')