I am trying to retrieve embed documents related to the key review looping them in a template using jinja with no success.
Currently the show_reviews variable is retrieving the exact product as I can see from its print on the terminal but I can't reach the review and loop it within the template.
app.py:
@app.route('/review/product_id?=<id>', methods=['POST', 'GET'])
def review(id):
now = datetime.datetime.now()
name=session['name']
post=request.form.get('review')
reviews = mongo.db.products.find_one({"_id": ObjectId(id)})
if request.method == 'POST':
mongo.db.products.find_one_and_update({"_id": ObjectId(id)},{
'$push':{'review':{
'name': name,
'post': post,
'date': now.strftime("%d-%m-%Y")
}
}
}
)
see_review = mongo.db.products
show_reviews = list(see_review.find({"_id": ObjectId(id)}))
print(show_reviews)
return render_template(
'product.html',
reviews=reviews,
name=name, date=now,
post=post,
show_reviews=show_reviews
)
product.html:
<div class="card-body">
<!--LOOP TO DISPLAY THE COMMENTS HERE-->
{% for rw in show_reviews %}
<p>{{ rw.post }}</p>
<small class="text-muted">Posted by {{ rw.name }} on {{ rw.date }}</small>
<hr>
{% endfor %}
<!-- ENDFOR HERE-->
<hr>
<!--FORM-->
{%if session['email'] != None %}
<form action="{{url_for('review', id=reviews._id)}}" method="POST">
<div class="form-group green-border-focus">
<textarea class="form-control" id="review" name='review' placeholder="Add Review" rows="3" required></textarea>
</div>
<button class="btn btn-success" type="submit">Leave a Review</button>
</form>
{% endif %}
</div>
