简体   繁体   English

如何呈现从 django 模板中的过滤器返回的 HTML?

[英]How do I render HTML returned from a filter in django template?

I have a md file which needs to be converted into html (for blog website).我有一个需要转换为 html 的 md 文件(用于博客网站)。 The filter (markdownify) converts md file into html tags.过滤器 (markdownify) 将 md 文件转换为 html 标签。 Now, html tags are shown on the website.现在,html 标签显示在网站上。 How do I render this html on website (in django template).我如何在网站上呈现这个 html(在 django 模板中)。

Is there any filter to do so?是否有任何过滤器可以这样做? Or is there any other method to convert md into html in django?或者有没有其他方法可以在django中将md转换为html?

Code ->代码 ->

<p>{{ post.body | markdownify }}</p>

md file -> md 文件 ->

### h1 header

*jh*

* sdfs
* ksdjfh
*  skdjkfhsk

[link] (#)

The output in web page:网页中的输出:

h1 header
<em>jh</em>

sdfs
ksdjfh
skdjkfhsk
[link] (#)

Maybe try {{ post.body | markdownify | safe }}也许试试{{ post.body | markdownify | safe }} {{ post.body | markdownify | safe }} {{ post.body | markdownify | safe }} . {{ post.body | markdownify | safe }} The safe filter disables any further HTML escaping.安全过滤器禁用任何进一步的 HTML 转义。 Read more about the filter in the Django documentation .Django 文档中阅读有关过滤器的更多信息。

Adding below code in the settings.py solved the problem.在 settings.py 中添加以下代码解决了问题。 HTML tags are not shown on rendered web page now. HTML 标签现在不会显示在呈现的网页上。

MARKDOWNIFY_WHITELIST_TAGS = {
 'a', 'p', 'h1', 'h2', 'h3','h4', 'h5', 'h6', 'h7', 'ul', 'li', 'span', 'img', 'div', 'abbr', 'acronym', 'em', 'blockquote', 'i', 'strong', 'ol', 'b', 'code'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM