Django tables2似乎显示了顶行和最后一行的重复结果。
E.g。
数据库记录显示:
ID|Name|User
1|test1|1
2|test2|1
但网站上显示的表格是:
ID|Name|User
2|test2|1
1|test1|1
2|test2|1
不确定为什么会这样。能否请你帮忙?
views.py
class ReportsView(ListView):
model = Reports
template_name = 'reports.html'
def get_context_data(self, **kwargs):
context = super(ReportsView, self).get_context_data(**kwargs)
login_user = self.request.user
context['reports_table'] = ReportsTable(Reports.objects.filter(user=login_user))
return context
models.py
class Reports(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=30)
description = models.CharField(max_length=50)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('edit_report', kwargs={'pk': self.pk})
tables.py
class ReportsTable(tables.Table):
class Meta:
model = Reports
attrs = {'class': 'table table-bordered'}
reports.html
{% load render_table from django_tables2 %}
{% render_table reports_table %}