web2py-ckeditor 如何显示由 ckeditor 创建的富文本而不是 html 标签

web2py-ckeditor how to display the rich text created by ckeditor instead of the html tags

我正在使用 web2py,ckeditor.I 想使用 SQLFORM 将富文本输入到数据库中。所以我选择ckeditor作为字段widget。我已经安装了 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))

以下是我的默认设置。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

当我点击 "view" 按钮时,我得到以下信息: enter image description here

我不想显示带有 html 标签的文本。我想获取富文本。任何人都可以告诉我我应该做什么或需要我选择另一个富文本编辑器吗?

通过下面的方法你告诉我点击"view"按钮可以显示富文本。但是,当我单击 "edit" 按钮时,会显示 HTML 标签代码。有什么方法可以在点击"edit"按钮时显示富文本吗?非常感谢你。

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

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

represent 函数控制字段值在 SQLTABLESQLFORM.grid、只读 SQLFORM 中以及使用 [=17] 时的显示方式=]方法。

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