开发者

How to render links in my CharField/TextField DB column as HTML in Django

开发者 https://www.devze.com 2023-02-20 15:36 出处:网络
A snippet of my template - {% block content %} {{ messag开发者_开发百科e.subject }} {{ message.content }}

A snippet of my template -

{% block content %}
    {{ messag开发者_开发百科e.subject }}
    {{ message.content }}
{% endblock %}

My message.content = " Check this out - /<a href="http://RigWave.In"> RigWave </a> "

If you want to render as HTML (as a link) like this -

" Check this out - RigWave "


After looking at your unedited post, I'm wondering if you replaced the link tags with [ to prevent SO from rendering it as a link (though `` takes care of it).

If you actually have properly formatted links in the CharField, you need to mark the string as safe to prevent auto HTML escaping.

{{ message.content|safe }}

or

{% autoescape off %}
    {{ body }}
{% endautoescape %}


http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters first sample is perfect, you could use custom filter or use .replace

0

精彩评论

暂无评论...
验证码 换一张
取 消