使用flask和Pymongo,在将评论添加到其他用户帖子时遇到问题

时间:2019-09-04 20:13:50

标签: python mongodb flask pymongo

我正在尝试将帖子/评论添加到其他用户的帖子中,但没有成功。

我有两个@app.route功能,其中一个已定义为一旦用户单击产品卡链接就在product.html模板上显示产品。

第二个功能review()用于在显示产品的同一页面上提交时显示评论(与博客文章中的评论相同)。

因此,当我单击以在product.html模板中提交评论时,jinja不允许我将帖子下方的评论也接收到MongoDB集合中。

app.py

#1st FUNCTION TO VIEW PRODUCT BY ITS ID            
@app.route('/product/product_id?=<id>', methods=['GET', 'POST'])
def product(id):
    view_product=mongo.db.products.find_one({"_id": ObjectId(id)})
    return render_template('product.html', view_product=view_product)

# 2nd FUNCTION REVIEW 
@app.route('/review/product_id?=<id>', methods=['POST', 'GET'])
def review(id):
    now = datetime.datetime.now()
    name=session['name']
    #email=session['email']
    post=request.form.get('review')
    reviews = mongo.db.products
    if request.method == 'POST':
        reviews.insert_one({'_id':ObjectId(id)}, 
        {'review': {
            'name': name,
            'post': post,
            'date': now.strftime("%Y-%m-%d")
            }
        })
    return render_template('product.html', reviews=reviews, name=name, date=now, post=post)

product.thml 模板:

<div class="card mt-4">
          <img class="card-img-top img-fluid" src="{{view_product.url}}" alt="">
           <div class="card-body">
            <h3 class="card-title">{{view_product.product_name}}</h3>
            <h4>${{view_product.price}}</h4>
            <p class="card-text">{{view_product.product_description}}</p>
            <p><strong>Seller: </strong>{{view_product.seller}}</p>
            4.0 stars
          </div>
        </div>
        <!-- /.card -->
        <div class="card card-outline-secondary my-4">
          <div class="card-header">
            Product Reviews
          </div>
          <div class="card-body">
            <!--LOOP TO DISPLAY THE COMMENTS HERE-->
            {% for review in reviews%}
            <p>{{ review.post }}</p>
            <small class="text-muted">Posted by {{ review.name }} on {{ review.date }}</small>
            <hr>
            {% endfor %}
            <!-- ENDFOR HERE-->
            <hr>
            <!--CONDITIONAL TO ALLOW ONLY LOGGED USERS COMMENT-->
            {% if session['email'] != None%}
            <form action="{{ url_for('review', id=view_product._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 %}
            <!--ENDIF-->
          </div>

产品集合设计:

_id:objectId("5d52fd39b403c051fbdcd")
category_name:"Home & Garden"
product_name:"Inox Table"
price:"500"
url:"<img URL here>"
product_description:"Great inox table to use in your living room."

单击提交按钮后出现的错误消息: pymongo.errors.OperationFailure: user is not allowed to do action [bypassDocumentValidation] on [DB_ecommerce_project.products]。我不确定,但这可能是因为表格设计或我试图将评论发送到products集合中的原因?

问题解决后,我想发送评论并在帖子下方看到它,在发表评论之后,我将看到用户名,帖子和发送时间。

0 个答案:

没有答案