gpt4 book ai didi

python - flask 错误 : "Method Not Allowed The method is not allowed for the requested URL"

转载 作者:IT老高 更新时间:2023-10-28 20:32:49 34 4
gpt4 key购买 nike

每当我尝试将数据提交到我的 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)

关于python - flask 错误 : "Method Not Allowed The method is not allowed for the requested URL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20689195/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com