How do I output HTML in a message in the new Django messages framework?

后端 未结 8 1118
花落未央
花落未央 2020-12-04 21:37

I\'m trying to display a bit of html in a message that\'s being displayed via the new Django messages framework. Specifically, I\'m doing this via the ModelAdmin.message_use

8条回答
  •  天涯浪人
    2020-12-04 22:08

    Another option is to use extra_tags keyword arg to indicate that a message is safe. Eg

    messages.error(request, 'Here is a link', extra_tags='safe')
    

    then use template logic to use the safe filter

    {% for message in messages %}
        
  • {% if 'safe' in message.tags %}{{ message|safe }}{% else %}{{ message }}{% endif %}
  • {% endfor %}

提交回复
热议问题