acess each row before rendering django tables2
Clash Royale CLAN TAG#URR8PPP
acess each row before rendering django tables2
I am using djangotables2 to simply render my table but I want to check some condition on each row of a column and print some addition to that value
but I can not find anyway to do so
Is it posible in djangotables2?
view
class BowlerList(LoginRequiredMixin, PagedFilteredTableView):
model = Bowlers
template_name = 'bowlers_list.html'
table_class = BowlerTable
filter_class = BowlerListFilter
formhelper_class = BowlerListFormHelper
def get_context_data(self, **kwargs):
context = super(BowlerList, self).get_context_data(**kwargs)
context['select_list'] = UserSelect.objects.filter(user=self.request.user)
return context
table
class BowlerTable(dt2.Table):
name = tables.LinkColumn('bowler-detail', args=[A('pk')])
class Meta:
model = Bowlers
fields = ('name', 'ahprank', 'pcarank')
template_name = 'django_tables2/bootstrap-responsive.html'
attrs = 'class': 'table table-striped table-bordered table-hover'
per_page = 10
template
% render_table table %
i need to compare Name column values with the value I will be getting in my "select_list" which I am getting through the views as context data
1 Answer
1
According to the documentation you can customize the way each cell is rendered. Check it here: https://django-tables2.readthedocs.io/en/latest/pages/custom-data.html#table-render-foo-methods
basically you would add the render_<column_name>
method and do your stuff there. Like this:
render_<column_name>
class BowlerTable(dt2.Table):
name = tables.LinkColumn('bowler-detail', args=[A('pk')])
class Meta:
model = Bowlers
fields = ('name', 'ahprank', 'pcarank')
template_name = 'django_tables2/bootstrap-responsive.html'
attrs = 'class': 'table table-striped table-bordered table-hover'
per_page = 10
def render_name(self, record):
# Your code here
The only thing extra that you might need, is to pass the select_list
in the initialization (__init__
) of the table object, to be able to access it in the new method.
select_list
__init__
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.