Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django filters. is_safe doesn't work

Tags:

python

django

In my app teams/templatetags/teams_extras.py I have this filter

from django import template

register = template.Library()

@register.filter(is_safe=True)
def quote(text):
    return "« {} »".format(text)

So I use it into my view teams/templates/teams/show.html

{% extends './base.html' %}
{% load static %}
{% load teams_extras %}
...
<b>Bio :</b> {{ team.biography|quote }}
...

But this is the result on my page:

&laquo; <p>The Miami Heat are an American professional basketball team based in Miami. The Heat compete in the National Basketball Association as a member of the league's Eastern Conference Southeast Division</p> &raquo; 

Why ? Thank you

like image 666
Alexandre Avatar asked Jan 23 '26 14:01

Alexandre


1 Answers

Doc says:

This flag tells Django that if a “safe” string is passed into your filter, the result will still be “safe” and if a non-safe string is passed in, Django will automatically escape it, if necessary.

So try to pass safe value into your filter:

{{ team.biography|safe|quote }}

or user mark_safe:

from django.utils.safestring import mark_safe

@register.filter()
def quote(text):
    return mark_safe("&laquo; {} &raquo;".format(text))

And:

{{ team.biography|quote }}

This should works.

like image 105
neverwalkaloner Avatar answered Jan 25 '26 03:01

neverwalkaloner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!