【发布时间】:2021-03-16 14:22:33
【问题描述】:
我正在使用 IFC,我需要从 .ifc 文件中提取一些数据以显示在 Django 模板中。
模板:
{% extends 'base.html' %}
{% block content %}
<h2>upload</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="document">
<button type="submit">Upload File</button>
</form>
<br>
{{result}}
{% endblock %}
我尝试了两种方法:
OpenIfcFile = ifcopenshell.open(filepath) #select the file
BrowseIfcProducts = OpenIfcFile.by_type('IfcElement') #search for the attribute 'IfcElement'
for eachproduct in BrowseIfcProducts:
#add the .ifc searched data to dict 'context' with 'result' key
context['result'] =
[
eachproduct.is_a(),
eachproduct.GlobalId,
eachproduct.get_info()["type"],
eachproduct.get_info().get("Name","error"),
]
return render(request, 'upload.html', context)
在这种方法中,由于键“结果”在每次迭代中都不会改变,只存储最后一个项目,在我的模板中我看不到所有项目。
所以,为了解决这个问题,我将结果键连接到一个迭代器 (i):
i = 1
for eachproduct in BrowseIfcProducts:
context['{} {}'.format('result', i)] = [
eachproduct.is_a(),
eachproduct.GlobalId,
eachproduct.get_info()["type"],
eachproduct.get_info().get("Name","error"),
]
i += 1
return render(request, 'upload.html', context)
但是很遗憾,模板中没有显示任何内容
我卡在这里,我不知道如何在模板上显示数据,有人可以帮助我吗?
【问题讨论】:
标签: python django dictionary ifc