Skip to content

Commit

Permalink
Refs django#15667 -- Added resetting default renderer when FORM_RENDE…
Browse files Browse the repository at this point in the history
…RER is changed.
  • Loading branch information
felixxm authored Sep 29, 2023
1 parent fe835c2 commit 6ad0dbc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
8 changes: 8 additions & 0 deletions django/test/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ def static_finders_changed(*, setting, **kwargs):
get_finder.cache_clear()


@receiver(setting_changed)
def form_renderer_changed(*, setting, **kwargs):
if setting == "FORM_RENDERER":
from django.forms.renderers import get_default_renderer

get_default_renderer.cache_clear()


@receiver(setting_changed)
def auth_password_validators_changed(*, setting, **kwargs):
if setting == "AUTH_PASSWORD_VALIDATORS":
Expand Down
6 changes: 6 additions & 0 deletions docs/topics/testing/tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1502,11 +1502,17 @@ Overridden settings Data reset
================================= ========================
USE_TZ, TIME_ZONE Databases timezone
TEMPLATES Template engines
FORM_RENDERER Default renderer
SERIALIZATION_MODULES Serializers cache
LOCALE_PATHS, LANGUAGE_CODE Default translation and loaded translations
STATIC_ROOT, STATIC_URL, STORAGES Storages configuration
================================= ========================

.. versionchanged:: 5.1

Resetting the default renderer when the ``FORM_RENDERER`` setting is
changed was added.

Isolating apps
--------------

Expand Down
4 changes: 0 additions & 4 deletions tests/forms_tests/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5145,22 +5145,19 @@ def test_custom_renderer_template_name(self):
class Person(Form):
first_name = CharField()

get_default_renderer.cache_clear()
t = Template("{{ form }}")
html = t.render(Context({"form": Person()}))
expected = """
<div class="fieldWrapper"><label for="id_first_name">First name:</label>
<input type="text" name="first_name" required id="id_first_name"></div>
"""
self.assertHTMLEqual(html, expected)
get_default_renderer.cache_clear()

@override_settings(FORM_RENDERER="forms_tests.tests.test_forms.CustomRenderer")
def test_custom_renderer_field_template_name(self):
class Person(Form):
first_name = CharField()

get_default_renderer.cache_clear()
t = Template("{{ form.first_name.as_field_group }}")
html = t.render(Context({"form": Person()}))
expected = """
Expand All @@ -5169,7 +5166,6 @@ class Person(Form):
<input type="text" name="first_name" required id="id_first_name">
"""
self.assertHTMLEqual(html, expected)
get_default_renderer.cache_clear()

def test_per_form_template_name(self):
class Person(Form):
Expand Down
3 changes: 0 additions & 3 deletions tests/template_tests/test_autoreloader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pathlib import Path
from unittest import mock

from django.forms.renderers import get_default_renderer
from django.template import autoreload
from django.test import SimpleTestCase, override_settings
from django.test.utils import require_jinja2
Expand Down Expand Up @@ -74,8 +73,6 @@ def test_form_template_reset_template_change_reset_call(self, mock_loader_reset)
def test_form_template_reset_template_change_no_djangotemplates(
self, mock_loader_reset
):
self.addCleanup(get_default_renderer.cache_clear)
get_default_renderer.cache_clear()
template_path = Path(__file__).parent / "templates" / "index.html"
self.assertIs(autoreload.template_changed(None, template_path), True)
mock_loader_reset.assert_not_called()
Expand Down

0 comments on commit 6ad0dbc

Please sign in to comment.