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

后端 未结 4 1436
感动是毒
感动是毒 2021-02-05 19:13

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,

4条回答
  •  孤城傲影
    2021-02-05 19:51

    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:

    class SomeTableView(SingleTableView):
        model = SomeModel
        template_name = 'test.html'
        table_class = SomeTable
    

    Template:

     {% load render_table from django_tables2 %}
     {% render_table table %}   
    

    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 %} 
    

提交回复
热议问题