From 54b7fded869522334c4988a2373032733d676c30 Mon Sep 17 00:00:00 2001
From: David Lesieur <github@davidlesieur.com>
Date: Tue, 22 Jul 2025 11:57:33 -0400
Subject: [PATCH] Update test for id attribute in Django 5.2 error template.
 Fixes #317. (#321)

* Update test for id attribute in Django 5.2 error template. Fixes #317.

* Extract version check into a variable to avoid duplicated logic.
---
 pyproject.toml           |  2 ++
 testing/testapp/tests.py | 34 ++++++++++++++++++++++++++--------
 tox.ini                  |  2 ++
 3 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/testing/testapp/tests.py b/testing/testapp/tests.py
index f6054db..b724b04 100644
--- a/testing/testapp/tests.py
+++ b/testing/testapp/tests.py
@@ -1,7 +1,7 @@
 import datetime
-
 from unittest import mock
 
+import django
 from django.conf import global_settings
 from django.conf import settings
 from django.core.exceptions import ImproperlyConfigured
@@ -135,17 +135,35 @@ def test_autoscape_with_form_errors(self):
         template = self.env.from_string("{{ form.name.errors }}")
         result = template.render({"form": form})
 
-        self.assertEqual(result,
-                         ("""<ul class="errorlist"><li>Ensure this value """
-                          """has at most 2 characters (it has 3).</li></ul>"""))
+        major, minor, _, _, _ = django.VERSION
+        is_django_5_2_or_later = major > 5 or (major == 5 and minor >= 2)
+
+        if is_django_5_2_or_later:
+            self.assertEqual(result,
+                            ("""<ul class="errorlist" id="id_name_error">"""
+                             """<li>Ensure this value has at most 2 characters"""
+                             """ (it has 3).</li></ul>"""))
+        else:
+            self.assertEqual(result,
+                            ("""<ul class="errorlist">"""
+                             """<li>Ensure this value has at most 2 characters"""
+                             """ (it has 3).</li></ul>"""))
 
         template = self.env.from_string("{{ form.errors }}")
         result = template.render({"form": form})
 
-        self.assertEqual(result,
-                         ("""<ul class="errorlist"><li>name<ul class="errorlist">"""
-                          """<li>Ensure this value has at most 2 characters (it """
-                          """has 3).</li></ul></li></ul>"""))
+        if is_django_5_2_or_later:
+            self.assertEqual(result,
+                            ("""<ul class="errorlist">"""
+                             """<li>name<ul class="errorlist" id="id_name_error">"""
+                             """<li>Ensure this value has at most 2 characters"""
+                             """ (it has 3).</li></ul></li></ul>"""))
+        else:
+            self.assertEqual(result,
+                            ("""<ul class="errorlist">"""
+                             """<li>name<ul class="errorlist">"""
+                             """<li>Ensure this value has at most 2 characters"""
+                             """ (it has 3).</li></ul></li></ul>"""))
 
     def test_autoescape_01(self):
         template = self.env.from_string("{{ foo|safe }}")
