Skip to content

Commit

Permalink
Implement .encode() to allow Starlette Response to consume a htpy ele…
Browse files Browse the repository at this point in the history
…ment directly.
  • Loading branch information
pelme committed Aug 24, 2024
1 parent c9e8b1e commit 23a0aee
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions htpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ def _iter_context(self, ctx: dict[Context[t.Any], t.Any]) -> Iterator[str]:
def __repr__(self) -> str:
return f"<{self.__class__.__name__} '{self}'>"

# Allow starlette Response.render to directly render this element without
# explicitly casting to str:
# https://github.com/encode/starlette/blob/5ed55c441126687106109a3f5e051176f88cd3e6/starlette/responses.py#L44-L49
def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes:
return str(self).encode(encoding, errors)

# 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:
Expand Down

0 comments on commit 23a0aee

Please sign in to comment.