Rendering newlines in user-submitted content (Python web app)

前端 未结 4 2044
死守一世寂寞
死守一世寂寞 2020-12-11 23:20

I have a web.py app that takes input from a textarea and inputs it to a database. I can get the information from the database and post it to the page but the NEWLINES are g

4条回答
  •  清歌不尽
    2020-12-12 00:07

    Two main ways to do this. The easiest one is to wrap the output in

     which will format it as entered.

    Or, you can replace newlies with
    (and not with

    ) as the characters represent a line break and not a paragraph.

    For the second option, this is one approach:

    >>> s
    'hello\nthere\r\nthis\n\ris a test'
    >>> r = '
    ' >>> s.replace('\r\n',r).replace('\n\r',r).replace('\r',r).replace('\n',r) 'hello
    there
    this
    is a test' >>>

    Or the third option - which is to use one of the many text entry libraries/formats and render the content through them (as mentioned by others - like markdown).

    However, that would be overkill if all you want to do is a simple replace.

提交回复
热议问题