本文介绍了python烧瓶模板返回前150个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{/ p>我想在我的网站上创建元描述,在这一刻我有这个功能获得产品说明: {product.description | nl2br}}
需要编辑这个函数才能得到前150个字符元描述,这是可能的模板?或者需要在views.py中创建这个函数?
解决方案
jinja2有一个名为 truncate
,文档是:
{{foo bar baz| truncate(9)}}
- > foo ...
所以,您可以试试这个:
{{product.description | truncate(150)}}
I want to create meta description on my website, in this moment i have this function for get product description :
{{ product.description|nl2br }}
Need edit this function for get only the first 150 characters for create meta description, is possible this in template ? or need create this function in views.py ?
解决方案
jinja2 has a filter named truncate
, the documentation is here:
{{ "foo bar baz"|truncate(9) }}
-> "foo ..."
so, you can try this one:
{{ product.description | truncate(150) }}
这篇关于python烧瓶模板返回前150个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!