views:

532

answers:

2

How can I turn off Django's automatic HTML escaping, when I write into model's TextField?

+1  A: 

One way to do it is to put a function in your model which returns the data marked as safe:

from django.utils.safestring import mark_safe 

class MyModel(models.Model): 
    myTextField = models.TextField()

def display_mySafeField(self): 
    return mark_safe(self.myTextField)

Then in the template you would have to use:

{{instance.display_mySafeField}}
Daniel Vassallo
That's, exactly what I needed :) Thank you very much.
Shark
If you use django's safe filter you do not need to modify the model at all.
bjunix
+4  A: 

You can just use django's safe filter. In your template you would do something like this:

{{ instance.my_text_field|safe }}
bjunix