Skip to content

Commit

Permalink
Fix django template injection/attribute handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelme committed Apr 16, 2024
1 parent 2a18ee8 commit 74e54f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions htpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ def __iter__(self) -> Iterator[str]:
def __repr__(self) -> str:
return f"<{self.__class__.__name__} '{self}'>"

# Avoid having Django "call" a htpy element that is injected into a
# template. Setting do_not_call_in_templates will prevent Django from doing
# an extra call:
# https://docs.djangoproject.com/en/5.0/ref/templates/api/#variables-and-lookups
do_not_call_in_templates = True


class Element(BaseElement):
def __getitem__(self: ElementSelf, children: Node) -> ElementSelf:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

def test_template_injection() -> None:
t = Template("<ul>{{ stuff }}</ul>")
result = t.render(Context({"stuff": li["I am safe!"]}))
result = t.render(Context({"stuff": li(id="hi")["I am safe!"]}))

assert result == "<ul><li>I am safe!</li></ul>"
assert result == '<ul><li id="hi">I am safe!</li></ul>'


def test_SafeString() -> None:
Expand Down

0 comments on commit 74e54f1

Please sign in to comment.