Flask Form-仅提交 returns GET 方法而不是 POST 方法
Flask Form-Submit only returns GET method instead of POST method
所以我正在尝试构建某种名为 FoodStock 的迷你社交媒体平台(用于学习目的)。我制作了一个页面,您可以在其中创建一个新的 post,它使用 new_post.html
文件作为渲染模板。在 new_post.html
文件中有一个 form
,无论我尝试什么,它都只有 return 是 GET
方法。虽然在提交表单的时候明明是要表单return一个POST
方法,但是它总是returnsGET
。一般来说,我是 Flask 和 webdev 的新手,所以我真的不太了解它。希望有人能帮忙。
这是我点击提交表单按钮时显示的内容。
127.0.0.1 - - [18/Jul/2021 14:45:07] "POST /home/2/helloworld HTTP/1.1" 302 -
GET
127.0.0.1 - - [18/Jul/2021 14:45:07] "GET /create_post/2/helloworld HTTP/1.1" 200 -
127.0.0.1 - - [18/Jul/2021 14:45:08] "GET /static/css/main.css HTTP/1.1" 200 -
主要代码如下所示。主路由工作正常,只是 create_post 路由始终接收 GET 方法。
@app.route('/home/<int:id>/<string:password>', methods=['GET', 'POST'])
def home(id, password):
if request.method == "POST":
return redirect(f"/create_post/{id}/{password}")
else:
return render_template("home_page.html", posts=FoodPost.query.all(), id=id, password=password)
@app.route('/create_post/<int:id>/<string:password>', methods=['GET', 'POST'])
def create_post(id, password):
if request.method == "POST":
response = request.form
new_post = FoodPost(title=response['title'], content=response['content'])
try:
db.session.add(new_post)
db.session.commit()
return redirect(f"/home/{id}/{password}")
except:
return "There was an issue processing your post, please try again later :("
else:
print(str(request.method))
return render_template("new_post.html", id=id, password=password)
if __name__ == "__main__":
app.run(debug=True)
new_post.html 下方文件
{% extends 'base.html' %}
{% block head %}
<title>Food Stock: New Post</title>
{% endblock %}
{% block body%}
<div>
<h1>New Post<h1>
</div>
<div class="form">
<form action='/home/{{id}}/{{password}}' method='POST'>
<label>Title</label>
<br>
<input type="text" name="title", id="title">
<br>
<br>
<label>Content</label>
<br>
<input type="text" name="content", id="content">
<input type="submit" value='Create Post' id="createpost">
</form>
<br>
<form action='/home/{{id}}/{{password}}'>
<input type="submit" value="Cancel" id="cancel">
</form>
</div>
{% endblock %}
此操作的问题 url.I 在您单击提交时为您提供工作流程
当您点击 new_post.html
中的提交按钮时,页面请求到 home
路由使用 POST
方法,home
路由函数将重定向为 redirect(f"/create_post/{id}/{password}")
用 GET
方法
你应该知道HTTP中的所有redirect
都是GET
方法,不管前面的方法是什么。
redirect(f"/create_post/{id}/{password}")
是告诉浏览器请求另一个给定的 url,因此浏览器请求 /create_post/{id}/{password}
使用 GET
方法。
所以你所有的日志历史都有意义。
{% extends 'base.html' %}
{% block head %}
<title>Food Stock: New Post</title>
{% endblock %}
{% block body%}
<div>
<h1>New Post<h1>
</div>
<div class="form">
<!-- The problem is here,the action url -->
<form action='/home/{{id}}/{{password}}' method='POST'>
<!-- Replace it with this url will be fine-->
<form action='/create_post/{{id}}/{{password}}' method='POST'>
<label>Title</label>
<br>
<input type="text" name="title", id="title">
<br>
<br>
<label>Content</label>
<br>
<input type="text" name="content", id="content">
<input type="submit" value='Create Post' id="createpost">
</form>
<br>
<form action='/home/{{id}}/{{password}}'>
<input type="submit" value="Cancel" id="cancel">
</form>
</div>
{% endblock %}
所以我正在尝试构建某种名为 FoodStock 的迷你社交媒体平台(用于学习目的)。我制作了一个页面,您可以在其中创建一个新的 post,它使用 new_post.html
文件作为渲染模板。在 new_post.html
文件中有一个 form
,无论我尝试什么,它都只有 return 是 GET
方法。虽然在提交表单的时候明明是要表单return一个POST
方法,但是它总是returnsGET
。一般来说,我是 Flask 和 webdev 的新手,所以我真的不太了解它。希望有人能帮忙。
这是我点击提交表单按钮时显示的内容。
127.0.0.1 - - [18/Jul/2021 14:45:07] "POST /home/2/helloworld HTTP/1.1" 302 -
GET
127.0.0.1 - - [18/Jul/2021 14:45:07] "GET /create_post/2/helloworld HTTP/1.1" 200 -
127.0.0.1 - - [18/Jul/2021 14:45:08] "GET /static/css/main.css HTTP/1.1" 200 -
主要代码如下所示。主路由工作正常,只是 create_post 路由始终接收 GET 方法。
@app.route('/home/<int:id>/<string:password>', methods=['GET', 'POST'])
def home(id, password):
if request.method == "POST":
return redirect(f"/create_post/{id}/{password}")
else:
return render_template("home_page.html", posts=FoodPost.query.all(), id=id, password=password)
@app.route('/create_post/<int:id>/<string:password>', methods=['GET', 'POST'])
def create_post(id, password):
if request.method == "POST":
response = request.form
new_post = FoodPost(title=response['title'], content=response['content'])
try:
db.session.add(new_post)
db.session.commit()
return redirect(f"/home/{id}/{password}")
except:
return "There was an issue processing your post, please try again later :("
else:
print(str(request.method))
return render_template("new_post.html", id=id, password=password)
if __name__ == "__main__":
app.run(debug=True)
new_post.html 下方文件
{% extends 'base.html' %}
{% block head %}
<title>Food Stock: New Post</title>
{% endblock %}
{% block body%}
<div>
<h1>New Post<h1>
</div>
<div class="form">
<form action='/home/{{id}}/{{password}}' method='POST'>
<label>Title</label>
<br>
<input type="text" name="title", id="title">
<br>
<br>
<label>Content</label>
<br>
<input type="text" name="content", id="content">
<input type="submit" value='Create Post' id="createpost">
</form>
<br>
<form action='/home/{{id}}/{{password}}'>
<input type="submit" value="Cancel" id="cancel">
</form>
</div>
{% endblock %}
此操作的问题 url.I 在您单击提交时为您提供工作流程
当您点击 new_post.html
中的提交按钮时,页面请求到 home
路由使用 POST
方法,home
路由函数将重定向为 redirect(f"/create_post/{id}/{password}")
用 GET
方法
你应该知道HTTP中的所有redirect
都是GET
方法,不管前面的方法是什么。
redirect(f"/create_post/{id}/{password}")
是告诉浏览器请求另一个给定的 url,因此浏览器请求 /create_post/{id}/{password}
使用 GET
方法。
所以你所有的日志历史都有意义。
{% extends 'base.html' %}
{% block head %}
<title>Food Stock: New Post</title>
{% endblock %}
{% block body%}
<div>
<h1>New Post<h1>
</div>
<div class="form">
<!-- The problem is here,the action url -->
<form action='/home/{{id}}/{{password}}' method='POST'>
<!-- Replace it with this url will be fine-->
<form action='/create_post/{{id}}/{{password}}' method='POST'>
<label>Title</label>
<br>
<input type="text" name="title", id="title">
<br>
<br>
<label>Content</label>
<br>
<input type="text" name="content", id="content">
<input type="submit" value='Create Post' id="createpost">
</form>
<br>
<form action='/home/{{id}}/{{password}}'>
<input type="submit" value="Cancel" id="cancel">
</form>
</div>
{% endblock %}