Disable HTML escaping in Django's TextField

后端 未结 3 1671
日久生厌
日久生厌 2020-12-05 02:17

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

3条回答
  •  再見小時候
    2020-12-05 02:57

    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): 
        my_textfield = models.TextField()
    
        def display_my_safefield(self): 
            return mark_safe(self.my_textfield)
    

    Then in the template you would have to use:

    {{ instance.display_my_safefield }}
    

提交回复
热议问题