简体   繁体   中英

python flask template return first 150 characters

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM