放入< a> django消息中的超链接 [英] Put a <a> hyperlink in a django message

查看:96
本文介绍了放入< a> django消息中的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django消息,并且想在其中添加超链接。

I'm using django messages and i want to put an hyperlink in it.

view.py:

from django.contrib import messages

def my_view(request):
    messages.info(request,"My message with an <a href='/url'>hyperlink</a>")

很明显,在我的页面中,我看到了html代码,没有超链接。

Obviously, in my page, i see the html code and no hyperlink. How to treat the message as an htlml code ?

希望这很清楚。

推荐答案

Django模板中的字符串会自动转义。您不希望原始HTML自动转义,因此您应该将字符串传递给 安全 过滤器:

Strings in Django templates are automatically escaped. You don't want your raw HTML to be auto-escaped, so you should either pass the string to the safe filter:

{{ message|safe }}

或使用 autoescape 标签:

or disable autoescape with the autoescape tag:

{% autoescape off %}
    {{ message }}
{% endautoescape %}

这篇关于放入&lt; a&gt; django消息中的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆