在用户输入上检测换行符(web2py) [英] Detecting newline character on user's input (web2py)

查看:50
本文介绍了在用户输入上检测换行符(web2py)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表:

db.define_table('comm',
                Field('post','reference post', readable=False, writable=False),
                Field('body','text', requires=IS_NOT_EMPTY()),
                auth.signature
                )

以及python函数中的以下代码:

and in a python function, the following code:

form=SQLFORM(db.comm).process()

我通过python函数在返回的视图中调用该表单

I call that form in the returned view by the python function

{{=form}}

问题是当用户输入两个或多个段落时,它没有检测到换行符.我该如何解决?

The problem is when the user inputs two or more paragraphs, it doesn't detect the newline character. How can I fix that?

推荐答案

假设您要引用视图中用户输入的后续显示,则可以使用< pre> 标记: http://www.w3schools.com/tags/tag_pre.asp .但是,您可能需要一些CSS才能获得所需的字体/样式(默认情况下,浏览器将使用固定宽度字体的替代样式).

Assuming you are referring to the subsequent display of the user input in a view, you could use a <pre> tag: http://www.w3schools.com/tags/tag_pre.asp. However, you might need some CSS to get the font/styling you like (by default, the browser will use alternative styling with a fixed-width font).

您还可以将换行符替换为< br> 标记:

You could also replace the newlines with <br> tags:

{{=XML(record.body.replace('\n', '<br>'), sanitize=True, permitted_tags=['br/'])}}

由于文本现在包含< br> HTML标记,因此有必要将其包装在 XML()中,以防止web2py转义HTML,但是您还希望清理文本并允许 < br> 标记以防止执行恶意代码.

Because the text now contains <br> HTML tags, it is necessary to wrap it in XML() to prevent web2py from escaping the HTML -- but you also want to sanitize the text and allow only <br> tags to prevent malicious code from being executed.

这篇关于在用户输入上检测换行符(web2py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆