如何解决Flask错误:“不允许使用方法所请求的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表单时,都会出现以下错误:
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}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。