formatting table cell content in django-tables2

谁说我不能喝 提交于 2020-01-01 20:01:30

问题


Love django-tables... but something that I'm sure is trivial to solve is giving me fits. When the value I pass for a given row/column is like:

some<br/>random<br/>words<br/>returned

I want the browser to parse and render the content in that cell... to look like this:

some
random
words
returned

not escape the content I'm passing and display it like this:

some<br/>random<br/>words<br/>returned

Surely there's some flag or option that I've missed?


回答1:


Use mark_safe as follows:

import django_tables2 as tables
from django.utils.safestring import mark_safe

class testTable(tables.Tables):
    id = tables.Column()
    html = tables.Column()

    def render_html(self):
        return mark_safe('some<br/>random<br/>words<br/>returned')

Same question was asked in this thread




回答2:


If some of your data already contains HTML, the simplest solution is to use a TemplateColumn rather than a normal column and mark the value as safe:

class Table(tables.Table):
    html_data = tables.TemplateColumn("{{ value|safe }}")
    # ...



回答3:


HA. Found it. It wasn't django-tables2 that was auto-escaping my content, it was the django templating system itself: https://code.djangoproject.com/wiki/AutoEscaping.

I had to change my template code to render the django-table2 like this:

{% autoescape off %}
    {% load render_table from django_tables2 %}
    {% render_table route_table %}
{% endautoescape %}


来源:https://stackoverflow.com/questions/12378717/formatting-table-cell-content-in-django-tables2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!