I am working with IFC and I need to extract some data from a .ifc file to be displayed in the Django template.
The template:
{% 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 %}
I've tried two methods:
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)
In this method, due the key 'result' doesn't change on each iteration, only the last item is stored and in my template i can't see all items.
So, to fix this problem, I concatenate the result key to a iterator (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)
But, unfortunately, nothing is displayed in the template
I'm stuck here, i don't know how can I show the data on template, can someone help me?
In the template you're referencing {{ result }}
, however it doesn't appear there is a key for result
in the context
dictionary you're passing to the view.
Since your keys seem arbitrary, you may just want a list. (Or likely you can just pass the BrowseIfcProducts
directly to the template if you dont need complex operators)
object_list = [[x.is_a(), z.GlobalId, x.get_info()["type"], x.get_info().get('name', 'Error')] for x in BrowseIfcProducts]
context['results'] = object_list
And in the template you can use
{{ results }}
A small FYI- if you're using the Django Debug Toolbar, in the "Templates" section you can see all the context availble to your template- which may help for future debugging.