Django error checking engine template?

I have a Django application that uses the Django template system to generate its (non-HTML) output, in addition, a web interface. There is a set of pages on which the user can create a template for the report by adding the {{}} tags to replace the variables and an additional templatetag library for convenient formatting.

However, the current way I do it is simple:

t = Template(component_template) self.output_content = t.render(component_context) 

Uses the default web output template engine. This string_if_invalid parameter string_if_invalid set to None and there are terrible warnings in the manual about violation of administration pages if you change it.

Thus, if the user receives either a typo in the variable name in the tag, it is quietly ignored and outputs it to the output. If they have a malformed tag, it actually kills the web application. I am looking for a way to validate a template during editing so that the user can be warned about the need to make changes.

What I'm aiming for is something like compiler output:

 unknown variable 'ffsdfd' on line 33 of template template syntax error on line 22 of template 

My first thought was to create a new Engine () template and use it for this purpose so that I can define the distinctive default string_if_invalid , but that does not say anything about the missing / invalid variable.

 engine = Engine(string_if_invalid="!!MISSING_VARIABLE!!", dirs=settings.TEMPLATES[0]['DIRS'], context_processors=settings.TEMPLATES[0]['OPTIONS']['context_processors'], app_dirs=settings.TEMPLATES[0]['APP_DIRS']) t = Template(component_template, engine=engine) try: self.output_content = t.render(component_context) except TemplateSyntaxError: pass # do something useful here to collect error messages 

The TemplateSyntaxError exception works, except that I do not get any context information, for example, where the error actually is, and, of course, I get only the first failure. Looking into the django.template code, it looks like there is some kind of extended exception inside that has a line number and a token that made it suffocate, but this does not escape the render () method.

So:

How can I provide useful error handling for errors in user-editable templates? Should I do it in a completely different way?

+5
source share
1 answer

This is how I solve it myself using a custom class and string_if_invalid . This gives you the name of the variable, but I'm sure you can tweak it further to get more information on contexts.

The global settings.py parameter should be easily adapted to your built-in example:

 class InvalidTemplateVariable(str): def __mod__(self,other): from django.template.base import TemplateSyntaxError raise TemplateSyntaxError("Invalid variable : '%s'" % other) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [....], 'APP_DIRS': True, 'OPTIONS': { 'string_if_invalid': InvalidTemplateVariable("%s"), 'context_processors': [ .... ], }, }, ] 

By the way, you can get more information on how / why this works in the next article (which I wrote) http://www.webforefront.com/django/customizedjangotemplates.html#stringifinvaliderror

+4
source

Source: https://habr.com/ru/post/1243574/


All Articles