如何使用jinja在模板中循环来自mongodb的嵌套文档?

时间:2019-09-07 15:23:49

标签: python mongodb flask

我正在尝试检索与密钥var http = require('http'); var fs = require('fs'); http.createServer(function(req, res) { fs.readFile('input.txt', function(err, data) { res.writeHead(200, { 'Content-Type': 'text/html' }); if(err) { console.log('Error: ', err); res.end(); } else { data = data && data.toString().replace(/\*/g, ''); res.write(data); res.end(); } }); }).listen(8080); 相关的嵌入文档,但使用jinja将其循环到模板中却没有成功。

目前,review变量正在检索确切的产品,正如我从终端上的印刷品中看到的那样,但是我无法获得评论并将其循环到模板中。

app.py:

show_reviews

product.html:

@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
    )

文件结构: enter image description here

1 个答案:

答案 0 :(得分:1)

好的。根据文档的结构,以下是组织代码的方式:

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>