You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dummy.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import string
  2. from django.conf import settings
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.template import Origin, TemplateDoesNotExist
  5. from django.utils.html import conditional_escape
  6. from .base import BaseEngine
  7. from .utils import csrf_input_lazy, csrf_token_lazy
  8. class TemplateStrings(BaseEngine):
  9. app_dirname = 'template_strings'
  10. def __init__(self, params):
  11. params = params.copy()
  12. options = params.pop('OPTIONS').copy()
  13. if options:
  14. raise ImproperlyConfigured(
  15. "Unknown options: {}".format(", ".join(options)))
  16. super().__init__(params)
  17. def from_string(self, template_code):
  18. return Template(template_code)
  19. def get_template(self, template_name):
  20. tried = []
  21. for template_file in self.iter_template_filenames(template_name):
  22. try:
  23. with open(template_file, encoding=settings.FILE_CHARSET) as fp:
  24. template_code = fp.read()
  25. except FileNotFoundError:
  26. tried.append((
  27. Origin(template_file, template_name, self),
  28. 'Source does not exist',
  29. ))
  30. else:
  31. return Template(template_code)
  32. raise TemplateDoesNotExist(template_name, tried=tried, backend=self)
  33. class Template(string.Template):
  34. def render(self, context=None, request=None):
  35. if context is None:
  36. context = {}
  37. else:
  38. context = {k: conditional_escape(v) for k, v in context.items()}
  39. if request is not None:
  40. context['csrf_input'] = csrf_input_lazy(request)
  41. context['csrf_token'] = csrf_token_lazy(request)
  42. return self.safe_substitute(context)