Debian Codegraph @

django/forms/renderers.py

raw
1 import functools
2 import warnings
3 from pathlib import Path
4
5 from django.conf import settings
6 from django.template.backends.django import DjangoTemplates
7 from django.template.loader import get_template
8 from django.utils.deprecation import RemovedInDjango60Warning
9 from django.utils.functional import cached_property
10 from django.utils.module_loading import import_string
11
12
13 @functools.lru_cache
14 def get_default_renderer():
15 renderer_class = import_string(settings.FORM_RENDERER)
16 return renderer_class()
17
18
19 class BaseRenderer:
20 form_template_name = "django/forms/div.html"
21 formset_template_name = "django/forms/formsets/div.html"
22 field_template_name = "django/forms/field.html"
23
24 bound_field_class = None
25
26 def get_template(self, template_name):
27 raise NotImplementedError("subclasses must implement get_template()")
28
29 def render(self, template_name, context, request=None):
30 template = self.get_template(template_name)
31 return template.render(context, request=request).strip()
32
33
34 class EngineMixin:
35 def get_template(self, template_name):
36 return self.engine.get_template(template_name)
37
38 @cached_property
39 def engine(self):
40 return self.backend(
41 {
42 "APP_DIRS": True,
43 "DIRS": [Path(__file__).parent / self.backend.app_dirname],
44 "NAME": "djangoforms",
45 "OPTIONS": {},
46 }
47 )
48
49
50 class DjangoTemplates(EngineMixin, BaseRenderer):
51 """
52 Load Django templates from the built-in widget templates in
53 django/forms/templates and from apps' 'templates' directory.
54 """
55
56 backend = DjangoTemplates
57
58
59 class Jinja2(EngineMixin, BaseRenderer):
60 """
61 Load Jinja2 templates from the built-in widget templates in
62 django/forms/jinja2 and from apps' 'jinja2' directory.
63 """
64
65 @cached_property
66 def backend(self):
67 from django.template.backends.jinja2 import Jinja2
68
69 return Jinja2
70
71
72 # RemovedInDjango60Warning.
73 class DjangoDivFormRenderer(DjangoTemplates):
74 """
75 Load Django templates from django/forms/templates and from apps'
76 'templates' directory and use the 'div.html' template to render forms and
77 formsets.
78 """
79
80 def __init__(self, *args, **kwargs):
81 warnings.warn(
82 "The DjangoDivFormRenderer transitional form renderer is deprecated. Use "
83 "DjangoTemplates instead.",
84 RemovedInDjango60Warning,
85 stacklevel=2,
86 )
87 super().__init__(*args, **kwargs)
88
89
90 # RemovedInDjango60Warning.
91 class Jinja2DivFormRenderer(Jinja2):
92 """
93 Load Jinja2 templates from the built-in widget templates in
94 django/forms/jinja2 and from apps' 'jinja2' directory.
95 """
96
97 def __init__(self, *args, **kwargs):
98 warnings.warn(
99 "The Jinja2DivFormRenderer transitional form renderer is deprecated. Use "
100 "Jinja2 instead.",
101 RemovedInDjango60Warning,
102 stacklevel=2,
103 )
104 super().__init__(*args, **kwargs)
105
106
107 class TemplatesSetting(BaseRenderer):
108 """
109 Load templates using template.loader.get_template() which is configured
110 based on settings.TEMPLATES.
111 """
112
113 def get_template(self, template_name):
114 return get_template(template_name)