目前,我正在将 jinja2 与 Flask 结合使用,并使用 ckeditor 在数据库中存储了一篇博客文章。
理想情况下,数据应首先显示图像,然后显示博客文章和外部链接到 flikr 的其他一些图像。
我知道我可以使用{{ post.body | safe}}
在单个帖子视图中将 html 显示为真实图像而不是 html 文本。
但是,我如何不显示 html,而只显示页面中帖子中的文本摘录,其中有多个指向不同 prost 和摘录的链接,而不显示图像 html。
在这种情况下,“这篇文章专门针对 xyz”应该是摘录
数据库body
= 列
<img alt="15189057555_7670752b57_o" class="main" src="https://farm6.staticflickr.com/5584/15189057555_7670752b57_o.jpg" style="width:100%;max-width:1920px"><p>This post is dedicated to xyz</p>
jinja2
'post' 是一个帖子对象。我试图将摘录长度限制为 100 个字母,不包含 html 标签和图像。
{{post.body[:100]}}...
将会呈现<img alt="15189057555_7670752b57_o" class="main" src="https://farm6.staticflickr.com/5584/1518905755...
以下是循环遍历所有帖子的代码摘录,以提供单个博客页面的链接、时间戳以及博客内容的摘录。
<h1>Latest Posts</h1>
{% if posts %}
{% for post in posts%}
<div class="post">
<h2><a href="post/{{post.postid}}">{{post.title}}</a></h2>
<h6>{{post.timestamp.strftime('%a %y/%m/%d')}}</h6>
<p>{{post.body[:100]}}...</p>
<p>Posted By: {{post.author.nickname}}</p>
</div>
{% endfor %}
{% else %}
<h4>No blog posts currently</h4>
{% endif%}
有更好的方法来设计这个吗?如果是这样,怎么办?请记住,我希望能够在一篇博客文章中插入多个图像和文本。
感谢你的帮助!