my_text = '''The template system works in a two-step process: compiling and rendering. A compiled template is, simply, a list of Node objects.
Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node (the compilation function), and what the node’s render() method does.'''
@register.filter(is_safe=True)
def format_description(description):
text = ''
for i in description.split('\n'):
text += ('<p class="site-description">' + i + '</p>')
return text
I get the output in raw html like so
<p class="site-description">The template system works in a two-step process: compiling and rendering. A compiled template is, simply, a list of Node objects.</p><p class="site-description"> </p><p class="site-description"> Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node (the compilation function), and what the node’s render() method does.</p>
instead of
The template system works in a two-step process: compiling and rendering. A compiled template is, simply, a list of Node objects.
Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node (the compilation function), and what the node’s render() method does.
The idea is to get the text and create different paragraph for each part of the list created after the split so the text can be formatted pretty and tightty
Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.
The safe filter indicates that the value is known to be safe and therefore does not need to be escaped. For example, given the following: blurb = '<p>You are <em>pretty</em> smart!</ p>' This would return unescaped HTML to the client: {{ blurb|safe }}
Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can’t modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one’s own need.
The Django template language has three ways of controlling what gets rendered: values, tags, and filters. Everything you put into a template that is not one of these three gets rendered as you have written it.
Django templates use tags and filters to define a mini-language that’s similar to Python—but isn’t Python. You’ll get to know Django templates through the tags and filters you use to compose reusable HTML.
You can extend the template engine by defining custom tags and filters using Python, and then make them available to your templates using the {% load %} tag. The most common place to specify custom template tags and filters is inside a Django app.
To disable autoescape you can use mark_safe
method:
from django.utils.safestring import mark_safe
@register.filter(is_safe=True)
def format_description(description):
text = ''
for i in description.split('\n'):
text += ('<p class="site-description">' + i + '</p>')
return mark_safe(text)
This is explicitly covered in the documentation: Filters and auto-escaping.
You need to mark the output as safe.
from django.utils.safestring import mark_safe
...
return mark_safe(text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With