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

后端 未结 8 1087
花落未央
花落未央 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:04

    You can use format_html. It applies escaping to all arguments.

    For example, if we can link with a 'mymodel' detail using an attribute call 'name':

    from django.contrib import messages
    from django.utils.html import format_html
    
    
    message = format_html("{} {}",
                          "This is the mymodel", 
                          reverse('myapp:mymodel-detail', args=(mymodel.id,)),
                          mymodel.name)
    messages.info(request, message)
    

    This answer is based on https://stackoverflow.com/a/33751717/3816639

提交回复
热议问题