html - 预期的表或查询集,而不是 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
解决方案
推荐阅读
- string - 为什么我不能将 Lines 迭代器收集到字符串向量中?
- ios - UIButton 背景图片可以支持动态类型吗?
- excel - Excel - 使用多个条件计算唯一值
- javascript - SVG 元素事件
- javascript - React/NodeJS - 访问 localhost:3000 时网页无法正常工作
- google-maps - 谷歌自动完成多边形作为 GeoJson
- java - 使用 Gradle 5.0 进行集成测试
- typescript - 如何在 TypeScript 中正确进行递归类型定义?
- javascript - 在整页脚本中使导航可点击
- cassandra - janusgraph 加载记录期间的提交性能低