Skip to content

Commit c92f976

Browse files
rubickczjieter
andcommitted
Add request to the table (#705)
Co-Authored-By: Jan Pieter Waagmeester <[email protected]>
1 parent 80fae05 commit c92f976

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

django_tables2/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ def configure(self, table):
6464
except EmptyPage:
6565
table.page = table.paginator.page(table.paginator.num_pages)
6666

67+
table.request = self.request
68+
6769
return table

docs/pages/custom-data.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ the `last_name` column::
103103
def render_name(self, value, record):
104104
return format_html("<b>{} {}</b>", value, record.last_name)
105105

106+
If you need to access logged-in user (or request in general) in your render methods, you can reach it through
107+
`self.request`::
108+
109+
def render_count(self, value):
110+
if self.request.user.is_authenticated():
111+
return value
112+
else:
113+
return '---'
114+
106115
.. important::
107116

108117
`render_foo` methods are *only* called if the value for a cell is determined to

tests/test_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ class SimpleTable(Table):
8383
table = SimpleTable([{}], request=request)
8484
self.assertTrue(table.columns["abc"].is_ordered)
8585

86+
def test_request_is_added_to_the_table(self):
87+
table = self.table()
88+
request = build_request("/")
89+
RequestConfig(request, paginate=False).configure(table)
90+
self.assertEqual(table.request, request)
91+
8692

8793
class NoPaginationQueriesTest(TestCase):
8894
def test_should_not_count_with_paginate_False(self):

0 commit comments

Comments
 (0)