2
2
from django .template .loader import get_template
3
3
from django .utils .html import strip_tags
4
4
5
+ from django_tables2 .utils import call_with_appropriate
6
+
5
7
from .base import Column , library
6
8
7
9
@@ -40,11 +42,19 @@ class ExampleTable(tables.Table):
40
42
41
43
empty_values = ()
42
44
43
- def __init__ (self , template_code = None , template_name = None , extra_context = None , ** extra ):
45
+ def __init__ (
46
+ self ,
47
+ template_code = None ,
48
+ template_name = None ,
49
+ context_object_name = "record" ,
50
+ extra_context = None ,
51
+ ** extra
52
+ ):
44
53
super ().__init__ (** extra )
45
54
self .template_code = template_code
46
55
self .template_name = template_name
47
56
self .extra_context = extra_context or {}
57
+ self .context_object_name = context_object_name
48
58
49
59
if not self .template_code and not self .template_name :
50
60
raise ValueError ("A template must be provided" )
@@ -56,11 +66,18 @@ def render(self, record, table, value, bound_column, **kwargs):
56
66
additional_context = {
57
67
"default" : bound_column .default ,
58
68
"column" : bound_column ,
59
- "record" : record ,
69
+ self . context_object_name : record ,
60
70
"value" : value ,
61
71
"row_counter" : kwargs ["bound_row" ].row_counter ,
62
72
}
63
- additional_context .update (self .extra_context )
73
+
74
+ extra_context = self .extra_context
75
+ if callable (extra_context ):
76
+ extra_context = call_with_appropriate (
77
+ extra_context ,
78
+ {"record" : record , "table" : table , "value" : value , "bound_column" : bound_column },
79
+ )
80
+ additional_context .update (extra_context )
64
81
with context .update (additional_context ):
65
82
if self .template_code :
66
83
return Template (self .template_code ).render (context )
@@ -75,3 +92,6 @@ def value(self, **kwargs):
75
92
"""
76
93
html = super ().value (** kwargs )
77
94
return strip_tags (html ).strip () if isinstance (html , str ) else html
95
+
96
+ def get_context_data (self , ** kwargs ):
97
+ return
0 commit comments