From 76ec032712d28dd8d4b9ad425f2b330a3e9a9109 Mon Sep 17 00:00:00 2001
From: Baptiste Mispelon <bmispelon@gmail.com>
Date: Fri, 22 Nov 2019 09:40:49 +0100
Subject: [PATCH] Refs #26281 -- Added a helpful error message for an invalid
 "r" specifier to dateformat.format().

---
 django/utils/dateformat.py           | 5 +++++
 tests/utils_tests/test_dateformat.py | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py
index 578572d38a..5bde2680b8 100644
--- a/django/utils/dateformat.py
+++ b/django/utils/dateformat.py
@@ -278,6 +278,11 @@ class DateFormat(TimeFormat):
 
     def r(self):
         "RFC 5322 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"
+        if type(self.data) is datetime.date:
+            raise TypeError(
+                "The format for date objects may not contain time-related "
+                "format specifiers (found 'r')."
+            )
         return self.format('D, j M Y H:i:s O')
 
     def S(self):
diff --git a/tests/utils_tests/test_dateformat.py b/tests/utils_tests/test_dateformat.py
index bffbcd3ba3..a77395cbf3 100644
--- a/tests/utils_tests/test_dateformat.py
+++ b/tests/utils_tests/test_dateformat.py
@@ -149,7 +149,7 @@ class DateFormatTests(SimpleTestCase):
     def test_invalid_time_format_specifiers(self):
         my_birthday = date(1984, 8, 7)
 
-        for specifier in ['a', 'A', 'f', 'g', 'G', 'h', 'H', 'i', 'P', 's', 'u']:
+        for specifier in ['a', 'A', 'f', 'g', 'G', 'h', 'H', 'i', 'P', 'r', 's', 'u']:
             msg = (
                 "The format for date objects may not contain time-related "
                 "format specifiers (found '%s')." % specifier