首页 > 解决方案 > PYTHON:我正在尝试从 XML 文件中读取不同的值并发送到 Django Html 模板

问题描述

我已经阅读了 xml 值,但我认为我用来存储数据的方式是错误的。我需要存储一个列表、一个字典或字典列表一些值,然后发送到 HTML 模板并呈现一个包含所有值的 HTML 表:

这应该是结果,下面的代码是我实际从 XML 读取值的方式

for files in file_in_path:
fileP = os.path.join(path,files)
tree = ET.parse(fileP)
roots = tree.getroot()
for x in roots.findall('DatiGeneraliDocumento'): //node of xml file that i need to read
    tipdoc = x.find('TipoDocumento').text //all the data
    numdoc = x.find('Numero').text 
    datadoc = x.find('Data').text
    mydict = {"tipdoc":item,"numdoc":numdoc,"datadoc":datadoc} //then i'm trying to save in dict
    liste.append(mydict)//and append all to a list
context = {"data":liste}

返回(响应,“page.html”,上下文)

当我在我的 html 页面中并尝试使用 django 格式语言“{{ data }}”打印结果时,他会打印出一个值而不是所有值,但是如果在 python 文件中我打印出我的列表,他会打印出所有列表。将所有值存储在所有 xml 文件中然后在 html 表中打印出来的最佳方法是什么?

标签: pythonhtmldjango

解决方案


推荐阅读