我正在尝试使用 django-tables2 为我的应用程序创建一些表,但遇到了一些困难。我使用的是 Python 2.7 和 Django 1.7。我正在按照教程进行操作,但遇到了问题。
我到了需要创建用于自定义的 Table 类的地步。但是,每当我这样做时,都会出现以下错误:
Expected table or queryset, not 'str'.
做完后some research看起来我使用的是旧版本的 django-tables2。但是,我昨天才使用
pip install django-tables2
安装了它并在半小时前更新它。知道如何让 django-tables2 正常工作吗?编辑 - 问题解决了。我正在使用
{% render_table people %}
而不是 {% render_table table %}
最佳答案
好吧,我认为您的问题不在于 django-tables2 的版本。在这里,我认为当您将变量从 View 传递到模板时,您传递的是字符串而不是查询集/表类对象。对于工作示例:
表类:
class SomeTable(tables.Table):
class Meta:
model= SomeModel
attrs = {"class": "paleblue"}
查看类:
class SomeTableView(SingleTableView):
model = SomeModel
template_name = 'test.html'
table_class = SomeTable
模板:
{% load render_table from django_tables2 %}
{% render_table table %} <!-- Here I am passing table class -->
或者您可以直接发送一个查询集来呈现表,如:
class SomeView(TemplateView):
def get(self, request, *args, **kwargs):
data = SomeModel.objects.all()
context = self.get_context_data(**kwargs)
context['table'] = data
return self.render_to_response(context)
并像这样渲染它:
{% load render_table from django_tables2 %}
{% render_table table %} <!-- Here I am passing queryset -->
关于python-2.7 - 使用 django-tables2 时出错 - 预期的表或查询集,而不是 'str',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683290/