【问题标题】:Method Not Allowed : The method is not allowed for the requested URLMethod Not Allowed : 请求的 URL 不允许该方法
【发布时间】:2020-06-16 18:34:43
【问题描述】:
@app.route('/<int:bookid>')

def view(bookid):

    if "user" in session:
        user = session["user"]


        review = ReviewForm(request.form)

**# ...get isbn,author,username,title from database**

      if request.method == 'POST':
            review = review.data['review']
            stars = review.data['stars']
            db.execute("INSERT INTO reviews (username,review,bookid,stars)  VALUES (:u,:r,:bi,:s)",{"u":user,"r":review,"bi":bookid,"s":stars})
            db.commit()

        rev=db.execute("SELECT EXISTS(SELECT REVIEW FROM reviews WHERE reviews.bookid = :id AND reviews.username =:username )",{"id":bookid,"username":user}).fetchone()[0]

**# ...get rating from api**

        results=[]

        res=db.execute("SELECT * from reviews WHERE bookid = :id",{"id":bookid})
        results=res.fetchall()

        if len(results) != 0:
               table = Reviews(results)
               table.border = True
               return render_template("bookdetails.html",rev=rev,user=user,title=booktitle,bookisbn=bookisbn,author=bookauthor,year=bookyear,table=table,form=review)

        else:
               return render_template("bookdetails.html",rev=rev,user=user,title=booktitle,bookisbn=bookisbn,author=bookauthor,year=bookyear,table="No Reviews",form=review)


    else:
        return redirect(url_for('login'))

Method Not Allowed : The method is not allowed for the requested URL error 在评论发布时出现

【问题讨论】:

  • 你能格式化你的代码吗?你能补充更多细节吗?

标签: flask sqlalchemy flask-wtforms


【解决方案1】:

您的@app.route('/') 需要包含methods 参数和POST 作为选项。默认情况下,这不包括在内,只允许 GET:

@app.route('/', methods=['GET', 'POST'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-01
    • 2020-08-27
    • 1970-01-01
    • 2016-01-22
    • 2021-03-15
    • 2022-11-02
    • 2023-03-12
    • 2012-06-22
    相关资源
    最近更新 更多