django-tables2:显示dict
我想显示一个目录列表,它不是来自模型。该列表是通过调用外部API(django-tables2:显示dict,django,django-tables2,Django,Django Tables2,我想显示一个目录列表,它不是来自模型。该列表是通过调用外部API(getapinfo())生成的。格言如下: [{"name": "testname", "value": 23}, {"name": "test2name": "value": 123}] class Index(SingleTableView): table_class = ProductTable
getapinfo()
)生成的。格言如下:
[{"name": "testname", "value": 23}, {"name": "test2name": "value": 123}]
class Index(SingleTableView):
table_class = ProductTable
login_url = '/login/'
def get_table_data(self):
return(GETAPIINFO())
我的views.py
如下所示:
[{"name": "testname", "value": 23}, {"name": "test2name": "value": 123}]
class Index(SingleTableView):
table_class = ProductTable
login_url = '/login/'
def get_table_data(self):
return(GETAPIINFO())
任何表格.py
:
将django-tables2导入为表
class ProductTable(tables.Table):
PName= tables.columns.TemplateColumn(template_code=u"""{{ record.name}}""", orderable=False, verbose_name='productName')
class Meta:
attrs = {"class": "table table-striped table-hover table-borderless",}
fields = ("PName",)
sequence = fields
我的模板:
{% extends 'base.html' %}
{% load render_table from django_tables2 %}
{% block content %}
{% render_table table %}
{% endblock content %}
这会导致错误:
。。。lib\site packages\django_tables2\templatetags\django_tables2.py”,第145行,在渲染中
raise VALUERROR(“应为表或查询集,而不是{}”。格式(klass))
ValueError:应为表或查询集,而不是列表
我已经有一段时间没有使用它了,但是根据django-tables2文档,数据输入应该接受一个列表
class Index(SingleTableView):
table_class = ProductTable
login_url = '/login/'
def get_table_data(self):
"""
Return the table data that should be used to populate the rows.
"""
return GETDATAFROMAPI()
您的ValueError发生在哪里?SingleTableView有一个get\u table\u data方法。您可以在视图中覆盖该方法并返回API数据。您可以添加导入吗?特别是对于ProductTable,您的模板中有什么?我无法从这里找出问题所在。很抱歉,我无法提供更多帮助。我将努力尝试我找出传递给templatetag的对象。您看到的ValueError是因为模板中的表变量是列表而不是表,这里:
{%render\u table%}
。这与您检索数据的方式无关。我注意到的一件事是,getapinfo
的示例数据写入错误,您有一个“:“where you should a”,”。