Take the 2-minute tour ×

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:


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


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


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

share|improve this question
Can you show me your Table code, and how you're supplying the HTML value to the table? –  bradley.ayers Sep 12 '12 at 20:51
For the sake of clarity, let's say all columns for the table are simply column_name = tables.Column() and that I'm messy enough to have the stored procedure output that's bound to the table supplying string values which include the markup. I was actually doing it a little differently, but I'm not sure that it matters in the context of this conversation. –  codemonkey Sep 13 '12 at 22:00
add comment

3 Answers

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

share|improve this answer
to return the variable rather than a static string, one needs to pass the value parameter, as in render_html(self, value) and return mark_safe(value + ' <b>more stuff</b>'). Took me a while to figure this out ;-) –  user1255933 Feb 19 at 19:57
add comment

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 }}")
    # ...
share|improve this answer
very useful if the data is not defined as a property. Regneel's solution only works if the data is defined as a property in the django model. –  user1255933 Feb 19 at 19:59
add comment

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 %}
share|improve this answer
This isn't the correct solution. –  bradley.ayers Sep 12 '12 at 20:51
I actually agree. It's a hack. I'm pretty sure there's some way to use a template to format individual cell contents, but I couldn't for the life of me get the bound data passed through when I tried it that way. –  codemonkey Sep 13 '12 at 22:05
I guess my "answer" should have made it clear that by "found it" I meant I'd figured out the cause for the behavior and a way to prevent it. A real "answer" would do something other than disabling autoescape... which is the default behavior for a good reason. –  codemonkey Sep 13 '12 at 22:13
add comment

Your Answer


