1+ from functools import cached_property
12import requests
23from django .template .backends .django import (
34 DjangoTemplates ,
@@ -54,6 +55,7 @@ def get_template(self, template_name):
5455
5556 text = _proxy_to_astro (template_name )
5657
58+ # form renderer and django template renderer have some differences
5759 if hasattr (self , "from_string" ):
5860 return self .from_string (text )
5961
@@ -67,15 +69,30 @@ def __init__(self, *args, **kwargs):
6769 astro_loader_path = "custom_admin.template_backends.AstroContentLoader"
6870
6971 if settings .DEBUG :
72+ # in debugging we don't want to cache the content
73+ # so we put the astro loader first
7074 self .engine .loaders = [astro_loader_path ] + self .engine .loaders
7175 else :
7276 # When running in production, put the astro loader
73- # in the Cached loader
77+ # in the Cached loader so we don't read the file every time
78+ # structure is: (cached_loader, [astro_loader, ... other loaders])
7479 self .engine .loaders [0 ] = (
7580 self .engine .loaders [0 ][0 ],
7681 [astro_loader_path ] + self .engine .loaders [0 ][1 ],
7782 )
7883
7984
8085class FormRenderer (GetTemplate , FormsDjangoTemplates ):
81- ...
86+ @cached_property
87+ def engine (self ):
88+ # Ugly hack because the form renderer is hardcoded here:
89+ # https://github.com/django/django/blob/fcd9d08379a2aee3b2c49eab0d0b8db6fd66d091/django/forms/renderers.py#L43
90+ # So we need to override the engine property to include the builtins
91+ engine = super ().engine
92+ engine .engine .builtins = (
93+ engine .engine .builtins + settings .TEMPLATES [0 ]["OPTIONS" ]["builtins" ]
94+ )
95+ engine .engine .template_builtins = engine .engine .get_template_builtins (
96+ engine .engine .builtins
97+ )
98+ return engine
0 commit comments