python-2.7 - 使用 django-tables2 时出错 - 预期的表或查询集,而不是 'str'

标签 python-2.7 django-1.7 django-tables2

我正在尝试使用 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/

相关文章:

python - 不包括前导零的随机数的排列

python - 如何合并 2 个带有命名空间的 xml 文件

python - Django 1.7 的重构可调用

python - Attrs 参数在 URLColumn 中不起作用(Django-tables2)

python - django 迁移在运行迁移之前运行 django table2 模块的过滤器是否正确?

python - 如何让 IDLE 接受 Unicode 字符的粘贴?

python - Windows 文件名被破坏

python - ContentType.objects.get_for_model(obj) 在代理模型对象上使用时返回基类模型

python - Django table2 调整单元格中图像的大小