如何提供博客摘要,而不必使用Jinja2模板显示HTML代码? [英] How do I provide a blog excerpt without having to show html code using Jinja2 Template?

查看:211
本文介绍了如何提供博客摘要,而不必使用Jinja2模板显示HTML代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



数据理想情况下首先显示一个图像,然后在数据库中显示一个图像。博客文章和一些其他图像,这些图像是外部链接到flikr。



我知道我可以使用 {{post.body |安全}} 里面的单个职位视图显示为一个真实的图像,而不是HTML文本的HTML。



但是,我怎么不显示的HTML,但只显示在文章中的帖子中的文本摘录,其中有多个链接到不同的prost和节选,没有图像的HTML显示。



在这种情况下这个职位是致力于xyz应该是摘录

数据库身体 =列

 < img alt =15189057555_7670752b57_oclass =mainsrc =https://farm6.staticflickr.com /5584/15189057555_7670752b57_o.jpgstyle =width:100%; max-width:1920px>< p>这篇文章致力于xyz< / p> 

jinja2



'post'是发布对象。我试图将摘录限制为100个字母,而不包含html标记和图片。

{{post.body [:100]} } ... 会显示< img alt =15189057555_7670752b57_oclass =mainsrc =https://farm6.staticflickr.com/5584/1518905755 .. 。



以下是代码摘录,循环显示所有帖子,提供指向单个博客页面,时间戳记和博客是关于什么的摘录。
$ b

 < h1>最新文章< / 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 /
< p> {{post.body [:100]}} ...< / p>
< p>发布通过:{{post.author.nickname}}< / p>
< / div>
{%endfor%}
{%else%}
< h4>目前没有博客文章< / h4>
{%endif%}

有没有更好的方法来设计这个?如果是这样,怎么样?请记住,我希望能够在一个博客文章中插入多个图像和文本。



感谢您的帮助!

解决方案

您必须查看Jinja的 striptags truncate 过滤器 http://jinja.pocoo.org/docs/dev/templates/#builtin-filters





 >>> from jinja2导入模板
>>> template = Template('blogpost:{{post | striptags}}!')
>>> template.render(post ='< img alt =15189057555_7670752b57_oclass =mainsrc =https://farm6.staticflickr.com/5584/15189057555_7670752b57_o.jpgstyle =width:100%; max-width :1920px>< p>这篇文章是致力于xyz< / p>)
u'blogpost:这篇文章是致力于xyz!'
pre>

在你的情况下,你想剥离标签,并限制为100个字符,所以替换

 < p为H. {{post.body [:100]}} ...< / p为H. 

by

 < p为H. {{post.body | striptags |截断(100)}}< / p为H. 


Currently I'm using jinja2 with flask and have stored a blog post using ckeditor in the database.

The data should ideally show an image first and then following the blog posts and some other images which are linked externally to flikr.

I know that I can use the {{ post.body | safe}} inside the single post view of to display the html as a real image instead of html text.

However, how do I NOT show the html but show only the text excerpt in the post in the page where there are multiple links to different prosts and excerpts without the image html showing up.

In this case "This post is dedicated to xyz" should be the excerpt

database body = column

<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' is an post object. I'm trying to limit the excerpt to 100 letters long without the html tags and images.

{{post.body[:100]}}... will show <img alt="15189057555_7670752b57_o" class="main" src="https://farm6.staticflickr.com/5584/1518905755...

The following is a code excerpt to loop through all posts to provide a link to a single blog page, a time stamp, and an excerpt of what the blog is about.

<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%}

Is there a better way to design this? If so, how? Please keep in mind I would like to be able to insert multiple images and text in the one blog post.

Thanks for all your help!

解决方案

You have to look at the striptags and truncate filter of Jinja http://jinja.pocoo.org/docs/dev/templates/#builtin-filters

Example:

>>> from jinja2 import Template
>>> template = Template('blogpost: {{ post|striptags }}!')
>>> template.render(post='<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>') 
u'blogpost: This post is dedicated to xyz!'

In your case, you want to strip tags, and limit to 100 chars, so replace

<p>{{post.body[:100]}}...</p>

by

<p>{{post.body|striptags|truncate(100)}}</p>

这篇关于如何提供博客摘要,而不必使用Jinja2模板显示HTML代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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