POST方法导致错误请求

时间:2017-12-25 10:43:37

标签: python python-3.x post flask http-status-code-400

编辑2:在HTML代码中添加了整个main元素

我正在使用Python,HTML,CSS和Jinja构建自己的网站。在这个页面上,我正在尝试构建一个函数,用户可以在给定的配方上发表评论和评论。

所以,我想发布以下代码,但它会显示以下错误消息:

  

“?[1m?[31mPOST /食谱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 %}

三个小时之后,我终于'承认'我坚持为什么它会给出错误400,因为它看起来像我的所有其他形式的动作。它显示31mPOST而我的其他表格动作显示37mPOST。也许这有帮助。

任何人都知道我做错了什么以及如何解决这个问题?

修改

我也试过在python代码中找到一个bug,但是找不到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)

1 个答案:

答案 0 :(得分:0)

此错误代码适用于错误请求。 您尝试获取三个元素(commentsratingrecipe_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 postAJAX)发布数据,这样就可以制作variable您想要的数据,然后发布。