python烧瓶模板返回前150个字符

编程入门 行业动态 更新时间:2024-10-23 19:21:49
本文介绍了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个字符

本文发布于:2023-11-10 22:00:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:烧瓶   字符   模板   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!