0da9c13192
Иерархия рамочных/дочерних договоров и платежей, интернет-биллинг, месячные IT-отчёты (импорт, DOCX/PDF, шаблоны выводов). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
25 lines
849 B
Python
25 lines
849 B
Python
"""Рендер строковых Django-шаблонов (без Jinja2 — уже есть в Django)."""
|
|
from __future__ import annotations
|
|
|
|
from django.template import Context, Engine, TemplateSyntaxError
|
|
|
|
|
|
class TemplateRenderError(ValueError):
|
|
"""Ошибка синтаксиса или рендера шаблона."""
|
|
|
|
|
|
_engine = Engine(
|
|
debug=False,
|
|
string_if_invalid='',
|
|
)
|
|
|
|
|
|
def render_django_string(source: str, context: dict) -> str:
|
|
try:
|
|
template = _engine.from_string(source or '')
|
|
return template.render(Context(context, autoescape=True))
|
|
except TemplateSyntaxError as exc:
|
|
raise TemplateRenderError(str(exc)) from exc
|
|
except Exception as exc: # noqa: BLE001 — отдаём пользователю понятный текст
|
|
raise TemplateRenderError(str(exc)) from exc
|