Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django filters to return HTML that will be rendered in the template

my_text

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.'''

my_filter

@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

My problem

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

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

like image 340
ThunderHorn Avatar asked Aug 23 '18 13:08

ThunderHorn


People also ask

What does filter do in Django template?

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.

What is safe filter in Django template?

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 }}

What are filters in Django template engine?

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.

How do I control what gets rendered in Django templates?

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.

What is a Django template?

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.

How do I extend the template engine in Django?

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.


2 Answers

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)
like image 164
neverwalkaloner Avatar answered Oct 30 '22 02:10

neverwalkaloner


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)
like image 35
Daniel Roseman Avatar answered Oct 30 '22 01:10

Daniel Roseman