首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >POST方法导致不良请求

POST方法导致不良请求
EN

Stack Overflow用户
提问于 2017-12-25 10:43:36
回答 1查看 128关注 0票数 0

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

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

因此,我想发布以下代码,但它提供了以下错误消息:

??[1M?[31 31mPOST /recipes HTTP/1.1?0m“400 -

代码语言:javascript
复制
{% 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错误,因为它看起来像我所有的其他形式的动作。它显示31 37mPOST,而我的其他表单操作显示37 37mPOST。也许这个有帮助。

有人知道我做错了什么吗?我怎样才能解决这个问题?

编辑

我还试图在python代码中找到一个bug,但找不到造成400错误的原因。由于问题可能在服务器端代码中,下面是代码:

代码语言:javascript
复制
@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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-25 16:45:35

此错误代码用于“坏”请求。您正在尝试获取三个元素(commentsratingrecipe_id),但它们不在表单中,因此flask抛出了一个400错误。

您可以做的是使用hidden inputs发送此类信息。

代码语言:javascript
复制
<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,然后发布它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47967872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档