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

问题描述 投票:8回答:2

我正在尝试使用django-tables2为我的应用程序创建一些表并遇到一些困难。我使用的是Python 2.7和Django 1.7。我正在按照教程,遇到了问题。

我到了需要创建Table类进行自定义的地步。但是,每当我这样做,我会收到以下错误:

预期的表或查询集,而不是'str'。

在做some research后,看起来我正在使用旧版本的django-tables2。但是,我昨天使用pip install django-tables2安装了它,并在半小时前更新了它。知道如何让django-tables2正常工作吗?

编辑 - 问题解决了。我使用{% render_table people %}而不是{% render_table table %}

python-2.7 django-1.7 django-tables2
2个回答
6
投票

我也遇到了这个问题。您应该做的第一件事是检查您的更新。 sudo pip install django-tables2 --upgrade sudo pip install django-tables2-reports --upgrade 升级也没有解决我的问题。 如果您已经升级了这些版本。您应该检查您的实施。如果您使用的是基于类的视图,并且您非常确定实现了视图,模板,表。你可能必须忘记网址。 所以网址应该是这样的。

/* I give the example with respect to other post*/
urls.py  /*Same dic with table.py,models..etc*/
from .views import SomeTableView   
urlpatterns = patterns('',
                   url(r"^$", SomeTableView.as_view(), name="index"),


                   )

如果它不是您网站的索引,您可能需要更改r“^ $”和name =“index”


4
投票

好吧,我认为你的问题不在于django-tables2的版本。在这里,我认为当您将视图中的变量传递给模板时,您将传递一个字符串而不是一个queryset / table类对象。对于工作示例:

表类:

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 -->
© www.soinside.com 2019 - 2024. All rights reserved.