字符串到HTML转换,以便页面可以读取HTML标记

时间:2016-05-20 20:23:31

标签: html django sqlite

我目前正在使用Django和SQLite开发后端博客。在我的设置中,我以这种形式将文章存储在数据库中:

wget https://github.com/schuhschuh/gflags/archive/master.zip
unzip master.zip
cd gflags-master
mkdir build && cd build
export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1
make 
sudo make install

在页面本身,我有使用会话数据的代码:

<p> <strong>The Time/Money Tradeoff</strong> </p> <p> As we flesh out High Life, Low Price, you will notice that sometimes we will suggest deals and solutions that may cost slightly more than their alternatives. We won’t always suggest the cheapest laptop...

我曾假设网络浏览器能够读取HTML标签。但是,它以该表单打印在页面上,带有可见的<p>{{request.session.article.0.blog_article}}</p> 标签等。我认为这是因为它在数据库中存储为Unicode字符串,并放在两个引号之间的页面上。如果我将HTML代码粘贴到页面上,格式看起来像我希望它看起来,但我希望它是一个自动化过程(告诉Django我想要哪个文章ID,它将页面的元素插入模板和一切看起来很棒)。

如何在页面可以看到HTML标记的表单中获取存储的文章?

1 个答案:

答案 0 :(得分:2)

默认情况下,django会自动调用模板中的所有字符串,因此当您在模板中呈现html代码时,它们只会显示为文字html代码。但您可以使用safe filter关闭此功能:

<p>{{request.session.article.0.blog_article|safe}}</p>