user2746078
user2746078

Reputation: 107

How To show variable as HTML in Django template?

let's said that I have function

def func(request):
    a="Hello, <br> this is my first app"
    return render_to_response(
                            'home.html',
                            {'a':a, 'request':request},
                            context_instance=RequestContext(request)
                          )

when I load variable a in home.html as

{{a}}

I will get Hello, <br> this is my first app , how to present <br> as new line in django template ...??

Upvotes: 1

Views: 2826

Answers (3)

Iliyan Bobev
Iliyan Bobev

Reputation: 3108

In the template file use {{a|safe}}, instead of {{a}}

Upvotes: 0

Selcuk
Selcuk

Reputation: 59444

An easier way would be to use

{{a|safe}}

in the template itself.

Upvotes: 1

Alvaro
Alvaro

Reputation: 12037

Try this:

from django.utils.safestring import mark_safe


a = mark_safe("Hello, <br> this is my first app")

Django automatically escapes any HTML in your variables, so you need to mark it as a safestring.

Keep in mind, for example, that safestring + unsafestring = unsafestring. You can read more about it here

Upvotes: 3

Related Questions