Search code examples
ckeditorweb2pyrich-text-editorweb2py-modules

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


I am using web2py with ckeditor.I want to input rich text into the database with SQLFORM. So I choose ckeditor as a field widget. I have installed the ckeditor and here is my 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))

and the following is my 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)

and the following is my index.html:

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

After I have input some information into the text with ckeditor widget the record is displayed by the SQLFORM.smartgrid like this: enter image description here

When I click the "view" button I get the following: enter image description here

I don't want to display the text with html tags. I want to get the rich text. Could any one tell me what should I do or need I choose another rich text editor?

With help of the following method you told me the rich text can be displayed when I click the "view" button. However the HTML tags codes are displayed when I click the "edit" button. Is there any method to show the rich text when I click the "edit" button? Thank you very much.


Solution

  • By default, all data included in a view is escaped (for security purposes). To override this, you can wrap the content in the XML() helper. To make this automatic, you can do it by specifying a represent attribute for the relevant field:

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

    The represent function controls how the field values will be displayed in SQLTABLE, SQLFORM.grid, read-only SQLFORMs, and when using the Rows.render method.

    The sanitize=True option limits the allowed HTML tags and attributes to minimize the security risk of including user-supplied markup in a page. See the documentation for details on customizing the allowed tags and attributes.