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.

fx XS13eK,u2CPgPClbjnk rQ6pycApTZnq92USU sMXRcm9N6A4SiWTKbb JI9T s khKoThmLHE1p6,Y,McxX FLxv8,Z00qPF5
eQD7Njp17 SJ9cXiBaXLeCm ITFoCjkc 1j coVkUE3 new9

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters