Bold text with asterisks
<p>In my Django project I want to make text bold if asterisks <code>*</code> are there at the start and end of text, the same feature we have here on Stack Overflow. Although I convert <code>**</code> to <code>&lt;b&gt;</code>, due to output escaping it becomes <code>&amp;lt;b&amp;gt;</code>. What is the right approach to achieve this?</p>

<p>template file contains <code>{{ anidea.description|format_text}}</code></p>

<p><code>format_text</code> is custom template filter</p>

<p>code..</p>

<pre><code>from django import template
from django.utils.safestring import mark_safe


register = template.Library()

@register.filter(name='format_text')
def custom_formating(value):
   for word in value.split():    

     start = word[:2]
     end = word[-2:]

     if start == '**' and end == '**':
        word = word[2:-2]        
        word = '&lt;b&gt;' +word+ '&lt;/b&gt;'
        mark_safe(word)    


  return value
</code></pre>

<p>I did it in following way.</p>

<p>views.py</p>

<pre><code>i.description = i.description.split()  #use of split()
</code></pre>

<p>template file (format_text is <code>custom template filter</code>)</p>

<pre><code>{% for text in anidea.description %}
     {{ text|format_text }}
{% endfor %} 
</code></pre>

<p>filter</p>

<pre><code>@register.filter(name='format_text')
def custom_formating(value):
 start = value[:2]
 end = value[-2:]

 if start == '**' and end == '**':
     value = value[2:-2]        
     value = '&lt;b&gt;' +value+ '&lt;/b&gt;'
     return mark_safe(value) 
 else:
     return value
</code></pre>

<p>with this way I can achieve output escaping for description and desired text formatting.</p>

python django