0

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>

The document structure: enter image description here

2
  • can you show the structure of your mongo document? Commented Sep 9, 2019 at 8:52
  • Hi, @Tobin I edited the post with the image of the document structure for a better understanding. Thank you. Commented Sep 9, 2019 at 17:22

1 Answer 1

1

Okay. Depending on the structure of your document, here is how you should organize your code:

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.find_one({"_id": ObjectId(id)})
    show_reviews = see_review['review']
    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>
Sign up to request clarification or add additional context in comments.

1 Comment

yeah, this is a great approach. However, I got a different approach, I looped the reviews var within the template such as {%for rw in reviews.review%} <p>{{rw.post}}</p><small class="text-muted">Posted by {{ rw.name }} on {{ rw['date']}}</small>{% endfor%} and it worked. Thank you very much @Tobin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.