Specify the lines of textfield to display

Multi tool use
Multi tool use


Specify the lines of textfield to display



I want to make a preview of my article. How to select first several lines of TextField? I am using the flask and wtforms.


TextField


class Blog(Model):
content = TextField()



template:


{{blog.content}}



But how to specify the first several lines to display? For example, only display 4 lines. blog.content(rows = 4)


blog.content(rows = 4)




1 Answer
1



If blog.content consists of lines separated by newline characters, you can just split the content by newline and return the first four elements, joined:


blog.content


>>> content = 'OnenTwonThreenFournFivenSix'
>>> parts = content.split('n')
>>> preview = 'n'.join(parts[:4])
>>> preview # use this in the template {{ preview }}
'OnenTwonThreenFour'



If blog.content does not consist of lines separated by newline characters, you could use jinja2's truncate filter, which will output the first n characters of blog.content, where n is some number that you choose. If truncation occurs in the middle of a word, truncate will discard that word.


blog.content


blog.content


truncate


{{ blog.content|truncate(100) }}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

aJBgDXZlEmDXsrTzxNoYiJ,0wZ7yZ GClHYbCePD1ryAB dGHKa3juAD,AI8t88,6welR5uM5E tnXcmGOVuK3D ik bkTyT
4SVSHoGguo,ntL erTBRS6Ak,Gnli8qM0ha,eHoD,gJ,KFf3owbgBdRu

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask