Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/django/forms/renderers.py: 76%
41 statements
« prev ^ index » next coverage.py v7.0.5, created at 2023-01-17 06:13 +0000
« prev ^ index » next coverage.py v7.0.5, created at 2023-01-17 06:13 +0000
1import functools
2from pathlib import Path
4from django.conf import settings
5from django.template.backends.django import DjangoTemplates
6from django.template.loader import get_template
7from django.utils.functional import cached_property
8from django.utils.module_loading import import_string
11@functools.lru_cache
12def get_default_renderer():
13 renderer_class = import_string(settings.FORM_RENDERER)
14 return renderer_class()
17class BaseRenderer:
18 # RemovedInDjango50Warning: When the deprecation ends, replace with
19 # form_template_name = "django/forms/div.html"
20 # formset_template_name = "django/forms/formsets/div.html"
21 form_template_name = "django/forms/default.html"
22 formset_template_name = "django/forms/formsets/default.html"
24 def get_template(self, template_name):
25 raise NotImplementedError("subclasses must implement get_template()")
27 def render(self, template_name, context, request=None):
28 template = self.get_template(template_name)
29 return template.render(context, request=request).strip()
32class EngineMixin:
33 def get_template(self, template_name):
34 return self.engine.get_template(template_name)
36 @cached_property
37 def engine(self):
38 return self.backend(
39 {
40 "APP_DIRS": True,
41 "DIRS": [Path(__file__).parent / self.backend.app_dirname],
42 "NAME": "djangoforms",
43 "OPTIONS": {},
44 }
45 )
48class DjangoTemplates(EngineMixin, BaseRenderer):
49 """
50 Load Django templates from the built-in widget templates in
51 django/forms/templates and from apps' 'templates' directory.
52 """
54 backend = DjangoTemplates
57class Jinja2(EngineMixin, BaseRenderer):
58 """
59 Load Jinja2 templates from the built-in widget templates in
60 django/forms/jinja2 and from apps' 'jinja2' directory.
61 """
63 @cached_property
64 def backend(self):
65 from django.template.backends.jinja2 import Jinja2
67 return Jinja2
70class DjangoDivFormRenderer(DjangoTemplates):
71 """
72 Load Django templates from django/forms/templates and from apps'
73 'templates' directory and use the 'div.html' template to render forms and
74 formsets.
75 """
77 # RemovedInDjango50Warning Deprecate this class in 5.0 and remove in 6.0.
79 form_template_name = "django/forms/div.html"
80 formset_template_name = "django/forms/formsets/div.html"
83class Jinja2DivFormRenderer(Jinja2):
84 """
85 Load Jinja2 templates from the built-in widget templates in
86 django/forms/jinja2 and from apps' 'jinja2' directory.
87 """
89 # RemovedInDjango50Warning Deprecate this class in 5.0 and remove in 6.0.
91 form_template_name = "django/forms/div.html"
92 formset_template_name = "django/forms/formsets/div.html"
95class TemplatesSetting(BaseRenderer):
96 """
97 Load templates using template.loader.get_template() which is configured
98 based on settings.TEMPLATES.
99 """
101 def get_template(self, template_name):
102 return get_template(template_name)