[英]How to fetch list of dictionary json data and show it on html page as table data
I have two columns data.One is "GLI Code" column and another is "Country" column.我有两列数据。一列是“GLI 代码”列,另一列是“国家”列。
I need to set the “GLI Code” data in “GLI Code” column.我需要在“GLI Code”栏中设置“GLI Code”数据。 “Country” data in “Country” column.
“国家”列中的“国家”数据。 here is my data in list of dictionary format.
这是我的字典格式列表中的数据。
views file:视图文件:
def tables_data(request):
dbfs_source_file_path = 'dbfs:/mnt/adls/MPF/Alternate_Currency_Keys_Aug.csv'
local_file_download_path = './mpf_dataset.csv'
dbfs_api = DbfsApi(api_client)
dbfs_path = DbfsPath(dbfs_source_file_path)
dbfs_api.get_file(dbfs_path, local_file_download_path, overwrite = True)
df = pd.read_csv(local_file_download_path).head(5)
json_records = df.reset_index().to_json(orient ='records')
data = []
data = json.loads(json_records)
return render(request, "home/tables-data.html", {'data':data})
data ouput:数据输出:
[{'index': 0, 'GLI Code': '15013256_U', 'Country': 'Indonesia', },
{'index': 1, 'GLI Code': '20061265_U', 'Country': 'Philippines'},
{'index': 2, 'GLI Code': '20063869_U', 'Country': 'Indonesia'}]
html file: html 文件:
<thead>
<tr>
{% for i in data %}
<th>{{i}}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for g in data.GLICode %}
<td>{{g}}</td>
{% endfor %}
</tr>
<tr>
{% for c in data.Country %}
<td>{{c}}</td>
{% endfor %}
</tr>
</tbody>
Above html code not giving me the expected output like below screenshot data.上面的 html 代码没有给我预期的 output 如下截图数据。
I want to set the data as below screenshot format.我想将数据设置为如下截图格式。
I think it will be better to use ajax request Instead of it please Visit: https://www.google.com/amp/s/www.geeksforgeeks.org/handling-ajax-request-in-django/amp/我认为最好使用 ajax 请求而不是它,请访问: https://www.google.com/amp/s/www.geeksforgeeks.org/handling-ajax-request-in-django/amp/
But the main problem with your code is that you are trying to get key in HTML which in is not quite correct you can see an example Visit: https://www.appsloveworld.com/django/100/431/how-to-display-json-items-in-django-templates但是您的代码的主要问题是您试图获取 HTML 中的密钥,这不太正确,您可以查看示例访问: https://www.appsloveworld.com/django/100/431/how-to-显示-json-items-in-django-templates
Doing something like this would work, but!做这样的事情会奏效,但是! you must remove the Space from 'GLI Code' in data.. Using Dictionary keys with spaces in Templates seems like a giant headache (extra custom template tags + stuff) - can be done, do not recommend
您必须从数据中的“GLI 代码”中删除空格。在模板中使用带有空格的字典键似乎令人头疼(额外的自定义模板标签 + 东西) - 可以,不推荐
<table>
<thead>
<tr>
<th></th>
<th>GLI Code</th>
<th>Country</th>
</tr>
</thead>
<tbody>
{% for i in data %}
<tr>
<th>{{i.index}}</th>
<th>{{i.GLICode}}</th>
<th>{{i.Country}}</th>
</tr>
{% endfor %}
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.