POST 方法导致错误请求
POST method results in Bad Request
编辑 2:在 HTML 代码中添加了整个主要元素
我正在使用 Python、HTML、CSS 和 Jinja 构建自己的网站。在此页面上,我尝试构建一个功能,用户可以在其中 post 评论和评论给定的食谱。
所以,我想 post 下面的代码,但它给出了这个错误消息:
"?[1m?[31mPOST /recipes HTTP/1.1?[0m" 400 -
{% block main %}
<div id="recipe_id" style="display: none;" name="recipe_id">
{% for recipe in recipes %}
{{ recipe.recipe_id }}
{% endfor %}
</div>
<table class="table table-striped">
{% for recipe in recipes %}
<h1>{{ recipe.recipe_name }}</h1>
<a href= {{ recipe.link_to_recipe }} ><h4>Link to recipe</h4></a>
<h5>{{ recipe.category }}</h5>
<h5>{{ recipe.review }}</h5>
{% endfor %}
</table>
<div>
<textarea name="comments" id="comment" type="text" autofocus="autofocus" form="commentrating">Give your comment</textarea>
</div>
<h3>Rate the recipe!</h3>
<form action="/recipes" method="post" id="commentrating">
<fieldset>
<div id="review">
<select name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<br>
<div>
<input type="submit" id="button" value="Submit">
</div>
</fieldset>
</form>
<br>
<table class="table table-striped" id="comments">
<thead>
<tr>
<th colspan="1">Posted by</th>
<th colspan="3">Comment</th>
</tr>
</thead>
{% for comment in comments %}
<tr>
<td colspan="1">{{ comment.user }}</td>
<td colspan="3">{{ comment.comments }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
三个小时后,我终于 'admitting' 明白为什么会出现错误 400,因为它看起来像我的所有其他表单操作。它显示 31mPOST,而我的其他表单操作显示 37mPOST。也许这有帮助。
有人知道我做错了什么以及我该如何解决这个问题吗?
编辑
我也试过在python代码中查找错误,但找不到400错误的原因。
由于问题可能出在服务器端代码中,因此代码如下:
@app.route("/recipes", methods=["POST"])
@login_required
def recipes():
"""Function where the comment and rating is added to the recipe."""
if request.method == "POST":
#get the comment and/or rating
comment = request.form["comments"]
rating = int(request.form["rating"])
recipe = int(request.form["recipe_id"])
db.execute("INSERT INTO comments (recipe_id, comments, user) \
VALUES (:recipe_id, :comments, :user)", \
recipe_id=recipe, comments=comment, user=session["user.id"])
givenrating = db.execute("SELECT reviews, number_of_reviews FROM recipes WHERE \
recipe_id=:recipe", recipe=recipe)
# check if there already is given a rating
if givenrating[0]["number_of_reviews"] == "None":
db.execute("UPDATE recipes SET review=:rating, number_of_reviews=:numb \
WHERE recipe_id=:recipe", recipe=recipe, rating=rating, numb=1)
#load chosen recipe
recipes = db.execute("SELECT * FROM recipes JOIN categories ON \
recipes.category = categories.cat_id WHERE recipe_id=:recipe", recipe=recipe)
#load comments of the recipe
comments = db.execute("SELECT * FROM comments JOIN users on \
comments.user = users.id WHERE recipe_id=:recipe", recipe=recipe)
return render_template("recipe.html", recipes=recipes, comments=comments)
else:
number = int(givenrating[0]["number_of_reviews"])
ratings = int(givenrating[0]["reviews"])
# update existing rating
fullrating = ratings * number
newrating = fullrating + rating
number += 1
averagerating = newrating / number
db.execute("UPDATE recipes SET review=:review, number_of_reviews=:newnumber \
WHERE recipe_id=:recipe", review=averagerating, newnumber=number, recipe=recipe)
#load chosen recipe
recipes = db.execute("SELECT * FROM recipes JOIN categories ON \
recipes.category = categories.cat_id WHERE recipe_id=:recipe", recipe=recipe)
#load comments of the recipe
comments = db.execute("SELECT * FROM comments JOIN users on \
comments.user = users.id WHERE recipe_id=:recipe", recipe=recipe)
return render_template("recipe.html", recipes=recipes, comments=comments)
此错误代码是针对错误请求的。
您正在尝试获取三个元素(comments
、rating
和 recipe_id
),但它们不存在于表单中,因此 flask
会引发 400 错误。
您可以使用 hidden inputs
发送此类信息。
<form action="/recipes" method="post" id="commentrating">
<input type="hidden" name="recipe_id" value="{{ recipe_id }}">
<textarea name="comments" id="comment" type="text" autofocus="autofocus" form="commentrating">Give your comment</textarea>
<fieldset>
<div id="review">
<select name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<br>
<div>
<input type="submit" id="button" value="Submit">
</div>
</fieldset>
</form>
但是,您应该以不同的方式设计代码,以便 recipe_id
是根据您要为其添加评论的食谱设置的。
您可以通过多种方式实现这一点,但由于这不在本问题的上下文中,所以我将单独保留该部分。
另一种方法是,使用 JS
代码(jquery post
或 AJAX
)到 post 数据,这样,您就可以制作一个variable
用你想要的数据,然后 post 它。
编辑 2:在 HTML 代码中添加了整个主要元素
我正在使用 Python、HTML、CSS 和 Jinja 构建自己的网站。在此页面上,我尝试构建一个功能,用户可以在其中 post 评论和评论给定的食谱。
所以,我想 post 下面的代码,但它给出了这个错误消息:
"?[1m?[31mPOST /recipes HTTP/1.1?[0m" 400 -
{% block main %}
<div id="recipe_id" style="display: none;" name="recipe_id">
{% for recipe in recipes %}
{{ recipe.recipe_id }}
{% endfor %}
</div>
<table class="table table-striped">
{% for recipe in recipes %}
<h1>{{ recipe.recipe_name }}</h1>
<a href= {{ recipe.link_to_recipe }} ><h4>Link to recipe</h4></a>
<h5>{{ recipe.category }}</h5>
<h5>{{ recipe.review }}</h5>
{% endfor %}
</table>
<div>
<textarea name="comments" id="comment" type="text" autofocus="autofocus" form="commentrating">Give your comment</textarea>
</div>
<h3>Rate the recipe!</h3>
<form action="/recipes" method="post" id="commentrating">
<fieldset>
<div id="review">
<select name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<br>
<div>
<input type="submit" id="button" value="Submit">
</div>
</fieldset>
</form>
<br>
<table class="table table-striped" id="comments">
<thead>
<tr>
<th colspan="1">Posted by</th>
<th colspan="3">Comment</th>
</tr>
</thead>
{% for comment in comments %}
<tr>
<td colspan="1">{{ comment.user }}</td>
<td colspan="3">{{ comment.comments }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
三个小时后,我终于 'admitting' 明白为什么会出现错误 400,因为它看起来像我的所有其他表单操作。它显示 31mPOST,而我的其他表单操作显示 37mPOST。也许这有帮助。
有人知道我做错了什么以及我该如何解决这个问题吗?
编辑
我也试过在python代码中查找错误,但找不到400错误的原因。 由于问题可能出在服务器端代码中,因此代码如下:
@app.route("/recipes", methods=["POST"])
@login_required
def recipes():
"""Function where the comment and rating is added to the recipe."""
if request.method == "POST":
#get the comment and/or rating
comment = request.form["comments"]
rating = int(request.form["rating"])
recipe = int(request.form["recipe_id"])
db.execute("INSERT INTO comments (recipe_id, comments, user) \
VALUES (:recipe_id, :comments, :user)", \
recipe_id=recipe, comments=comment, user=session["user.id"])
givenrating = db.execute("SELECT reviews, number_of_reviews FROM recipes WHERE \
recipe_id=:recipe", recipe=recipe)
# check if there already is given a rating
if givenrating[0]["number_of_reviews"] == "None":
db.execute("UPDATE recipes SET review=:rating, number_of_reviews=:numb \
WHERE recipe_id=:recipe", recipe=recipe, rating=rating, numb=1)
#load chosen recipe
recipes = db.execute("SELECT * FROM recipes JOIN categories ON \
recipes.category = categories.cat_id WHERE recipe_id=:recipe", recipe=recipe)
#load comments of the recipe
comments = db.execute("SELECT * FROM comments JOIN users on \
comments.user = users.id WHERE recipe_id=:recipe", recipe=recipe)
return render_template("recipe.html", recipes=recipes, comments=comments)
else:
number = int(givenrating[0]["number_of_reviews"])
ratings = int(givenrating[0]["reviews"])
# update existing rating
fullrating = ratings * number
newrating = fullrating + rating
number += 1
averagerating = newrating / number
db.execute("UPDATE recipes SET review=:review, number_of_reviews=:newnumber \
WHERE recipe_id=:recipe", review=averagerating, newnumber=number, recipe=recipe)
#load chosen recipe
recipes = db.execute("SELECT * FROM recipes JOIN categories ON \
recipes.category = categories.cat_id WHERE recipe_id=:recipe", recipe=recipe)
#load comments of the recipe
comments = db.execute("SELECT * FROM comments JOIN users on \
comments.user = users.id WHERE recipe_id=:recipe", recipe=recipe)
return render_template("recipe.html", recipes=recipes, comments=comments)
此错误代码是针对错误请求的。
您正在尝试获取三个元素(comments
、rating
和 recipe_id
),但它们不存在于表单中,因此 flask
会引发 400 错误。
您可以使用 hidden inputs
发送此类信息。
<form action="/recipes" method="post" id="commentrating">
<input type="hidden" name="recipe_id" value="{{ recipe_id }}">
<textarea name="comments" id="comment" type="text" autofocus="autofocus" form="commentrating">Give your comment</textarea>
<fieldset>
<div id="review">
<select name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<br>
<div>
<input type="submit" id="button" value="Submit">
</div>
</fieldset>
</form>
但是,您应该以不同的方式设计代码,以便 recipe_id
是根据您要为其添加评论的食谱设置的。
您可以通过多种方式实现这一点,但由于这不在本问题的上下文中,所以我将单独保留该部分。
另一种方法是,使用 JS
代码(jquery post
或 AJAX
)到 post 数据,这样,您就可以制作一个variable
用你想要的数据,然后 post 它。