B
B
bubublik2019-05-16 21:57:10
Django
bubublik, 2019-05-16 21:57:10

How to execute a tag in a template that was passed to it as a string?

Passed a variable to the Django template
x = 'Text {{ data }} text'
Called it in the template:
{{ x }}
It was displayed as
Text {{ data }} text
And how can I make {{ data }} from this line be processed as a tag and that the value from the data variable would be displayed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bubublik, 2019-05-17
@bubublik

I made a filter, suddenly someone will come in handy

from django import template

register = template.Library()

@register.filter(name='string_replace')
def string_replace(string, value):
    return string.replace('{{ data }}', value)

Usage in template:
{% load string_replace %}

{{ x | string_replace:data }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question