3👍
Making a text bold shouldn’t be done in the view. Indeed, the view should not take care of formatting (which is the role of templates). What you can do however, is adding an extra variable to the rendering context, and depending on its value, make the text bold or not in the template.
For example :
In the view :
#...
is_important = True if something else False
extra_context.update({'is_important': is_important})
#...
In the template :
...
{% if is_important %}<b>{{ text_to_render }}</b>{% else %}{{ text_to_render }}{% endif %}
...
But more generally, deciding if a text is bold or not, is not even formatting, rather styling and should not be made in the template (so you shouldn’t use the <bold> markup). So I would suggest :
...
<span {% if is_important %}class="is-important"{% endif %}>
{{ text_to_render }}
</span>
...
And a stylesheet :
.is-important{
font-weight: bold;
}
9👍
Django automatically escapes string values in templates, to improve security.
If you know that a value has HTML in it, and is trusted, then you can turn off escaping:
{{my_trusted_html|safe}}
- [Django]-Error when creating admin in django tutorial
- [Django]-Django giving error ImportError: No module named 'corsheaders'
- [Django]-Cannot Connect to Django from outside the Local Server
- [Django]-Running a Django 1.4-based app on Google App Engine
1👍
You can use the autoescape tag in a django template.
Also, if you write your own filter, you can control whether or not it is escaped.
- [Django]-Nginx/gunicorn connection hanging for 60 seconds
- [Django]-Django view based cache with authed users
- [Django]-How can this be written on a single line?