首页 > 解决方案 > 预期的表或查询集,而不是 str

问题描述

我正在尝试使用 django-tables2 呈现表,但我收到此错误消息:Expected table or queryset, not str

视图.py:

def getKpiTableCsd(request, report_id):
  template = loader.get_template('kpi/confirm_kpi_csd.html')
  report = get_object_or_404(KpiMembersCsd, id=report_id)
  context = RequestContext(request, {'report': report})
  table_context = KpiMembersCsdTable(KpiMembersCsd.objects.filter(
      id=report_id))  # creating table object not string
  return HttpResponse(template.render(context.flatten()),
                    {'table': table_context})

表.py:

import django_tables2 as tables
import kpi.models


class KpiMembersCsdTable(tables.Table):
   class Meta:
      model = kpi.models.KpiMembersCsd

追溯:

  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/flex/work/02_dolasilla/dolasilla_seite/kpi/views.py", line 32, in getKpiTableCsd
    return HttpResponse(template.render(context.flatten()),
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/flex/work/02_dolasilla/dolasilla/lib/python3.7/site-packages/django_tables2/templatetags/django_tables2.py", line 145, in render
    raise ValueError("Expected table or queryset, not {}".format(klass))
ValueError: Expected table or queryset, not str

标签: htmldjangodjango-tables2

解决方案


推荐阅读