Skip to content

Commit

Permalink
Add request to the table (#705)
Browse files Browse the repository at this point in the history
Co-Authored-By: Jan Pieter Waagmeester <[email protected]>
  • Loading branch information
rubickcz and jieter committed Oct 21, 2019
1 parent 80fae05 commit c92f976
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django_tables2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ def configure(self, table):
except EmptyPage:
table.page = table.paginator.page(table.paginator.num_pages)

table.request = self.request

return table
9 changes: 9 additions & 0 deletions docs/pages/custom-data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ the `last_name` column::
def render_name(self, value, record):
return format_html("<b>{} {}</b>", value, record.last_name)

If you need to access logged-in user (or request in general) in your render methods, you can reach it through
`self.request`::

def render_count(self, value):
if self.request.user.is_authenticated():
return value
else:
return '---'

.. important::

`render_foo` methods are *only* called if the value for a cell is determined to
Expand Down
6 changes: 6 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class SimpleTable(Table):
table = SimpleTable([{}], request=request)
self.assertTrue(table.columns["abc"].is_ordered)

def test_request_is_added_to_the_table(self):
table = self.table()
request = build_request("/")
RequestConfig(request, paginate=False).configure(table)
self.assertEqual(table.request, request)


class NoPaginationQueriesTest(TestCase):
def test_should_not_count_with_paginate_False(self):
Expand Down

0 comments on commit c92f976

Please sign in to comment.