【问题标题】:Django ckeditor and django tables2 safe renderDjango ckeditor 和 django tables2 安全渲染
【发布时间】:2018-10-20 14:59:24
【问题描述】:

我正在使用 django tables2 https://github.com/jieter/django-tables2 在模板中的表格中显示数据,该表格由 django-ckeditor https://github.com/django-ckeditor/django-ckeditor 创建。具体来说,我遇到的问题与 LinkColumn 表中的渲染有关:

class MyTable(tables.Table):
    notes = tables.LinkColumn('notes_update', kwargs={"pk": Accessor("pk")},attrs={'th': {'id': 'thnotes'}, 'td': {'id': 'tdnotes'}})

数据显示在带有 html <p> <p/> 标签的表格中,由 ckeditor 创建。

据我所知,我需要使用{{ |safe }} 标签在模板中呈现,以便显示没有html <p> <p/> 标签的表格数据,但我不知道如何与django 一起实现这一点tables2 因为我的表格在我的模板中呈现如下:

{% render_table table %}

我试过用以下方法包围它,但没有成功:

  {% autoescape off %}
  {% render_table table %}
  {% endautoescape %}

有没有人有任何想法可以帮助我在没有 html <p> <p/> 标签的情况下在我的表格中呈现 notes 数据?任何帮助将不胜感激!

【问题讨论】:

    标签: django ckeditor4.x django-tables2


    【解决方案1】:

    我认为最直接的方法是使用tables.TemplateColumn

    class MyTable(tables.Table):
        notes = tables.TemplateColumn(
            template_code='''<a href="{% url 'notes_update' pk=record.pk %}">{{ record.notes |safe }}</a>''',
            attrs={'th': {'id': 'thnotes'}, 'td': {'id': 'tdnotes'}}
        )
    

    这应该为您提供一个熟悉的模板来控制表格单元格的内容。

    【讨论】:

    • 绝妙的解决方案!太感谢了。它只是在 pk=record.pk %} 之后的 }> 之间缺少一个右双引号。也许您可以为未来的访问者编辑您的答案?
    • 谢谢,添加了缺少的报价。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2015-10-03
    • 2011-09-28
    • 1970-01-01
    相关资源
    最近更新 更多