Flask错误:“不允许使用方法所请求的URL不允许使用该方法”

每当我尝试将数据提交到Flask表单时,都会出现以下错误:

Method Not Allowed The method is not allowed for the requested URL.

我认为问题出在return redirect(url_for('database'))我正在做。我也尝试return

render_template('database.html)过。表单项提交到数据库后,我正在尝试调用数据库页面。

我的代码的相关部分如下:

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

def entry_page():

if request.method == 'POST':

date = request.form['date']

title = request.form['blog_title']

post = request.form['blog_main']

post_entry = models.BlogPost(date = date, title = title, post = post)

db.session.add(post_entry)

db.session.commit()

return redirect(url_for('database'))

else:

return render_template('entry.html')

@app.route('/database')

def database():

query = []

for i in session.query(models.BlogPost):

query.append((i.title, i.post, i.date))

return render_template('database.html', query = query)

entry.html是…

THIS IS THE BLOG ENTRY PAGE

blog:

<html>

<form action='/database' method = "post">

date<input name = "date" type = "text" class="text">

title<input name = "blog_title" type = "text" class="text">

main<input name = "blog_main" type = "text" class="text">

<input type = "submit">

</form>

</html>

和database.html …

THIS IS THE QUERY:

{{query}}

回答:

这里发生的是数据库路由不接受任何url方法。

我会尝试将url方法放在应用程序路由中,就像在entry_page函数中一样:

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

def entry_page():

if request.method == 'POST':

date = request.form['date']

title = request.form['blog_title']

post = request.form['blog_main']

post_entry = models.BlogPost(date = date, title = title, post = post)

db.session.add(post_entry)

db.session.commit()

return redirect(url_for('database'))

else:

return render_template('entry.html')

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

def database():

query = []

for i in session.query(models.BlogPost):

query.append((i.title, i.post, i.date))

return render_template('database.html', query = query)

以上是 Flask错误:“不允许使用方法所请求的URL不允许使用该方法” 的全部内容, 来源链接: utcz.com/qa/406511.html

回到顶部