Error Using django-tables2 - Expected table or queryset, not 'str'
Asked Answered
A

4

15

I am trying to create some tables for my application using django-tables2 and running into some difficulties. I am using Python 2.7, and Django 1.7. I am following the tutorial, and I ran into problems.

I reach the point where I need to create a Table class for customization. However, whenever I do so, I get the following error:

Expected table or queryset, not 'str'.

After doing some research it looks like I am using an older version of django-tables2. However, I just installed it yesterday using pip install django-tables2 and updated it a half an hour ago. Any idea how I can get django-tables2 to work properly?

EDIT - Problem solved. I was using {% render_table people %} instead of {% render_table table %}

Asaasabi answered 16/4, 2015 at 18:38 Comment(0)
T
8

I encountered that problem, too. The first thing you should do is to check your updates:

sudo pip install django-tables2 --upgrade
sudo pip install django-tables2-reports --upgrade

Upgrades didn't fix my problem either.
If you have already upgraded version of these. You should check your implementation. If you are using Class Based View and you probably implemented the view,template,table. You probably forget the urls:

/* 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"),

                   
                   )

If it is not index of your website you have to probably change the r"^$" and name="index"

Tana answered 17/4, 2015 at 11:51 Comment(0)
F
13

Well I think your problem isn't with the version of django-tables2. Here I think when you are passing a variable from view to template, you are passing a string instead of a queryset/table class object. For working example:

Table class:

class SomeTable(tables.Table):
    
    class Meta:
        model= SomeModel
        attrs = {"class": "paleblue"}

View Class:

                                       # Important part!!!!
class SomeTableView(SingleTableView):  # <-- check for SingleTableView
    model = SomeModel
    template_name = 'test.html'
    table_class = SomeTable

Template:

 {% load render_table from django_tables2 %}
 {% render_table table %}   <!-- Here I am passing table class -->

Or you can directly send a queryset to render the table like:

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)
         

and render it like this:

{% load render_table from django_tables2 %}
{% render_table table %} <!-- Here I am passing queryset -->
Folacin answered 17/4, 2015 at 6:55 Comment(1)
This is the best answer.Robeson
T
8

I encountered that problem, too. The first thing you should do is to check your updates:

sudo pip install django-tables2 --upgrade
sudo pip install django-tables2-reports --upgrade

Upgrades didn't fix my problem either.
If you have already upgraded version of these. You should check your implementation. If you are using Class Based View and you probably implemented the view,template,table. You probably forget the urls:

/* 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"),

                   
                   )

If it is not index of your website you have to probably change the r"^$" and name="index"

Tana answered 17/4, 2015 at 11:51 Comment(0)
L
5

Had the same problem. I forgot to add SingleTableMixin in the parameters of the view class

Lactase answered 11/7, 2019 at 7:3 Comment(2)
Similar to what Sharpless512 said, but in my case, I forgot to update my view from ListView to SingleTableViewHumid
that really helped :)Abase
B
0

According to django-tables2 documentation:
In tutorial/views.py, the class name is PersonListView here

I changed the class name from PersonListView to PersonTableView And also made the this change in urls.py.

My problem was solved by doing this.

Why was this problem caused? Please anyone let me know.

Boxboard answered 31/12, 2020 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.