I’m trying to pass the list of object to HTML for looping to render the data fetch from MongoDB
Approach tried:
-
Created a global variable as list of object. Fetched the data from the MongoDB and set the data to global variable
And tried to fetch it directly but this is not accessible means no error and there was no data as well.
-
Passing the fetch list of object as render during def of index(request)
x=mycol.find() downloadData=list(x) return render(request, 'index.html',downloadData)
error:
File “C:xxxviews.py”, line 30, in index
return render(request, ‘index.html’,downloadData)AttributeError: ‘list’ object has no attribute ‘dict‘
HTML Code:
{% for d in downloadData %} <div class="row content"> <h2>I Love Food</h2> <p>Food is my passion. Lorem ipsum dolor sit amet, consectetur adipiscing elit, </p> </div> {% endfor %}
Could anyone point what i am doing wrong in both approaches?
If you could point to correct link will be helpful
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You should pass your data as dictionary in context like this,
return render(request, 'index.html',{'downloadData'=downloadData})
and in HTML, you can access it like this,
{% for d in downloadData %}
<div class="row content">
<h2>d</h2>
</div>
{% endfor %}
Method 2
I think you should pass the downloadData as an argument like below
return render(request, 'index.html', {'downloadData': downloadData})
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0