1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #36410 -- Added support for Template Partials to the Django Template Language.

Introduced `{% partialdef %}` and `{% partial %}` template tags to
define and render reusable named fragments within a template file.
Partials can also be accessed using the `template_name#partial_name`
syntax via `get_template()`, `render()`, `{% include %}`, and other
template-loading tools.

Adjusted `get_template()` behavior to support partial resolution, with
appropriate error handling for invalid names and edge cases. Introduced
`PartialTemplate` to encapsulate partial rendering behavior.

Includes tests and internal refactors to support partial context
binding, exception reporting, and tag validation.

Co-authored-by: Carlton Gibson <carlton@noumenal.es>
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Co-authored-by: Nick Pope <nick@nickpope.me.uk>
This commit is contained in:
farhan
2025-06-03 10:32:34 +05:00
committed by nessita
parent fda3c1712a
commit 5e06b97095
16 changed files with 1587 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import pickle
from django.test import SimpleTestCase
from django.utils.datastructures import (
CaseInsensitiveMapping,
DeferredSubDict,
DictWrapper,
ImmutableList,
MultiValueDict,
@@ -367,3 +368,42 @@ class CaseInsensitiveMappingTests(SimpleTestCase):
with self.assertRaisesMessage(TypeError, msg):
self.dict1["New Key"] = 1
self.assertEqual(len(self.dict1), 2)
class DeferredSubDictTests(SimpleTestCase):
def test_basic(self):
parent = {
"settings": {"theme": "dark", "language": "en"},
"config": {"enabled": True, "timeout": 30},
}
sub = DeferredSubDict(parent, "settings")
self.assertEqual(sub["theme"], "dark")
self.assertEqual(sub["language"], "en")
with self.assertRaises(KeyError):
sub["enabled"]
def test_reflects_changes_in_parent(self):
parent = {"settings": {"theme": "dark"}}
sub = DeferredSubDict(parent, "settings")
parent["settings"]["theme"] = "light"
self.assertEqual(sub["theme"], "light")
parent["settings"]["mode"] = "tight"
self.assertEqual(sub["mode"], "tight")
def test_missing_deferred_key_raises_keyerror(self):
parent = {"settings": {"theme": "dark"}}
sub = DeferredSubDict(parent, "nonexistent")
with self.assertRaises(KeyError):
sub["anything"]
def test_missing_child_key_raises_keyerror(self):
parent = {"settings": {"theme": "dark"}}
sub = DeferredSubDict(parent, "settings")
with self.assertRaises(KeyError):
sub["nonexistent"]
def test_child_not_a_dict_raises_typeerror(self):
parent = {"bad": "not_a_dict"}
sub = DeferredSubDict(parent, "bad")
with self.assertRaises(TypeError):
sub["any_key"]