B
B
bamond2016-05-10 13:25:58
Django
bamond, 2016-05-10 13:25:58

How to remove shielding?

Good afternoon. The whole page is outputted from the database including {% for i in data %} {% endfor %} {{ data }}
the problem is that variable data and loops are output as plain text....
{% autoescape off %} - disabled. ..
please tell me how to make variables and loops appear as they should be, and not as text
Thank you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-05-10
@bamond

The data from the database is the data in the context, and the context is not parsed by the template engine. You will have to write a template tag for this:

from django import template
from django.utils.safestring import mark_safe

register = template.Library()

class RenderNode(template.Node):
    def __init__(self, content):
        self.content = content
    
    def render(self, context):
        try:
            self.content = template.resolve_variable(self.content, context)
            return template.Template(self.content).render(template.Context(context, autoescape=False))
        except template.TemplateSyntaxError as e:
            return mark_safe("<strong>Template error: There is an error one of this page's template tags: <code>%s</code></small>" % e.message)


@register.tag(name='render')
def render_django(parser, token):
    content = token.split_contents()[-1]
    return RenderNode(content)
render_django.is_safe = True

And then call it in the template
{% render flatpage.content %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question