【问题标题】:web2py-ckeditor how to display the rich text created by ckeditor instead of the html tagsweb2py-ckeditor如何显示由ckeditor创建的富文本而不是html标签
【发布时间】:2015-11-20 09:31:00
【问题描述】:

我正在使用带有 ckeditor 的 web2py。我想使用 SQLFORM 将富文本输入到数据库中。所以我选择 ckeditor 作为字段小部件。我已经安装了 ckeditor,这是我的 db.py:

from plugin_ckeditor import CKEditor
ckeditor = CKEditor(db)
db.define_table("comtable",
            Field("com_name",label="name"),
            Field("com_property",label="property",requires=IS_IN_SET(['A', 'B', 'C',"D"])),Field("com_detail",label="info",type="text",widget=ckeditor.widget))

以下是我的default.py/index:

def index():
    form=SQLFORM(db.comtable,fields = ['com_name',"com_property","com_detail"])
    gridform=SQLFORM.smartgrid(db.comtable)
    if form.process().accepted:
        response.flash="OK"
    return dict(form=form,gridform=gridform)

以下是我的index.html:

{{=form}}
{{=gridform}}

在我使用 ckeditor 小部件在文本中输入一些信息后,SQLFORM.smartgrid 将显示记录,如下所示: enter image description here

当我点击“查看”按钮时,我得到以下信息: enter image description here

我不想显示带有 html 标签的文本。我想获得富文本。谁能告诉我应该做什么或需要我选择另一个富文本编辑器吗?

通过以下方法,您告诉我,当我单击“查看”按钮时,可以显示富文本。但是,当我单击“编辑”按钮时,会显示 HTML 标记代码。单击“编辑”按钮时,有什么方法可以显示富文本?非常感谢。

【问题讨论】:

    标签: ckeditor web2py rich-text-editor web2py-modules


    【解决方案1】:

    默认情况下,视图中包含的所有数据都会被转义(出于安全目的)。要覆盖它,您可以将内容包装在 XML() 帮助程序中。要自动执行此操作,您可以通过为相关字段指定 represent 属性来实现:

    Field('com_detail', label='info', type='text', widget=ckeditor.widget,
          represent=lambda content, row: XML(content, sanitize=True))
    

    represent 函数控制字段值在SQLTABLESQLFORM.grid、只读SQLFORMs 以及使用Rows.render 方法时的显示方式。

    sanitize=True 选项限制允许的 HTML 标记和属性,以最大限度地降低在页面中包含用户提供的标记的安全风险。有关自定义允许的标签和属性的详细信息,请参阅documentation

    【讨论】:

    • 非常感谢。有用。你真是太好了,安东尼
    • 嗨,安东尼。按照上面的方法,点击“查看”按钮就可以显示富文本了。但是,当我单击“编辑”按钮时,会显示 HTML 标记代码。单击“编辑”按钮时,有什么方法可以显示富文本?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2014-03-02
    • 2017-07-23
    • 2012-03-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    相关资源
    最近更新 更多