Skip to content

Commit

Permalink
Added TemplateColumn.value() and enhanced export docs (fixes #470)
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Aug 23, 2017
1 parent 3a9604f commit 0457611
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
13 changes: 13 additions & 0 deletions django_tables2/columns/templatecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from django.template import Context, Template
from django.template.loader import get_template
from django.utils import six
from django.utils.html import strip_tags

from .base import Column, library

Expand Down Expand Up @@ -63,3 +65,14 @@ def render(self, record, table, value, bound_column, **kwargs):
return get_template(self.template_name).render(context.flatten())
finally:
context.pop()

def value(self, **kwargs):
'''
The value returned from a call to `value()` on a `TemplateColumn` is
the rendered tamplate with `django.utils.html.strip_tags` applied.
'''
html = super(TemplateColumn, self).value(**kwargs)
if isinstance(html, six.string_types):
return strip_tags(html)
else:
return html
4 changes: 3 additions & 1 deletion docs/pages/custom-data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ argument.
a default value is rendered instead (both `.Column.render` and
``Table.render_FOO`` are skipped).

.. _table.value_foo:

`Table.value_foo` methods
-------------------------

If you want to use `Table.as_values` to export your data, you might want to define
a method ``value_foo``, which is analogous to ``render_foo``, but used to render the
values rather than the HTML output.

Please refer to `~.Table.as_values` for an example.
Please refer to `.Table.as_values` for an example.

.. _subclassing-column:

Expand Down
17 changes: 17 additions & 0 deletions docs/pages/export.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ If you must use a function view, you might use someting like this::
'table': table
})

What exacly is exported?
------------------------

The export views use the `.Table.as_values()` method to get the data from the table.
Because we often use HTML in our table cells, we need to specify something else for the
export to make sense.

If you use :ref:`table.render_foo`-methods to customize the output for a column,
you should define a :ref:`table.value_foo`-method, returning the value you want
to be exported.

If you are creating your own custom columns, you should know that each column
defines a `value()` method, which is used in `Table.as_values()`.
By default, it just calls the `render()` method on that column.
If your custom column produces HTML, you should override this method and return
the actual value.


Excluding columns
-----------------
Expand Down
9 changes: 9 additions & 0 deletions tests/columns/test_templatecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,12 @@ class Table(tables.Table):

table = Table([{'track': 'Beat it {Freestyle}'}])
assert table.rows[0].get_cell('track') == 'track: Beat it {Freestyle}'


def test_should_strip_tags_for_value():
class Table(tables.Table):
track = tables.TemplateColumn('<span>{{ value }}</span>')

table = Table([{'track': 'Space Oddity'}])

assert list(table.as_values()) == [['Track'], ['Space Oddity']]

0 comments on commit 0457611

Please sign in to comment.