提交按钮给出400错误请求

时间:2019-01-15 19:16:12

标签: javascript python html flask

我正在设置我的网站,使其能够采用上载的文件和相关的名称,但是当给定有效的文件和名称时,提交按钮似乎不起作用。它在错误情况下有效,但在提供正确数据时无效。

该网站应该将上传的文件放入本地计算机上的文件夹(存在)中。它还应该更新我与sqlite3连接的数据库。

这是我正在处理的页面的HTML:

<div id="form">
            <form action="{{ url_for('algorithm') }}" method="POST" enctype="multipart/form-data">
                <div id="algo-header"><h1><u>New Algorithm:</u></h1></div>
                <div id="field-names">
                    User Name:
                    <br><br><br>
                    Algorithm:
                </div>
                <div id="fields">
                    <input class="text" type="text" id="usr" name="usr" />
                    <br><br><br>
                    <input class="text" type="file" id="file" name="file" />
                </div>
                <div id="flash-message">
                    {% with messages = get_flashed_messages() %}
                        {% if messages %}
                            <ul class=flashes style="padding: 0;">
                            {% for message in messages %}
                            <li style="list-style-type: none;">{{ message }}</li>
                            {% endfor %}
                            </ul>
                        {% endif %}
                    {% endwith %}
                </div>
                <div id="buttons">
                    <br>
                    <input class="submit" type="submit" style="width: 60px" value="Submit" />
                    <br><br>
                </div>
            </form>
            <button id="home-button" class="submit">Return to Homepage</button>
        </div>
        <br>
        <script type="text/javascript">
            document.getElementById("home-button").onclick =
                function() {
                    location.href = "{{ url_for('welcome') }}";
                };
        </script>

这是运行页面的函数:

@app.route("/algorithm", methods=["GET", "POST"])
def algorithm():
    if request.method == "POST":
        print("POST method invoked")
        if request.form["usr"] == "":
            print("No name given")
            flash("No name given")
            return redirect(request.url)
        # if no file is provided
        if request.form["file"] == "":
            print("No file given")
            flash("No file given")
            return redirect(request.url)
        else:
            print("Data is valid")
            if "file" not in request.files:
                flash("No file given")
                return redirect(request.url)
            file = request.files["file"]
            print("filename : {}".format(file.filename))
            if file.filename == "":
                print("Invalid file name")
                flash("Invalid file name")
                return redirect(request.url)
            if file and allowed_file(file.filename):
                print("Everything's good!")
                print("usr : {}".format(request.form["usr"]))
                usr = request.form["usr"]
                filename = secure_filename(file.filename)
                print("file.filename : {}".format(file.filename))
                print("filename : {}".format(filename))
                file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
                c = con.cursor()
                c.execute("INSERT INTO algorithms (user, source) VALUES (?,?)", (usr, filename))
                con.commit()
                msg = "Algorithm Added Successfully"
                con.close()
                return render_template("results.html", msg=msg)
               # return redirect(url_for("uploaded_file", filename=filename))   
    return render_template("new_algo.html")

当我单击提交按钮时,该功能应该检查是否存在名称,检查是否存在具有有效文件名的文件,然后如果满足这些条件,则将用户名和文件名添加到数据库中并将文件保存到本地文件夹。

该函数在前两个函数上起作用,并且在未提供名称/文件的情况下正确闪烁消息。但是,在我 do 给出一个名称并 do 提供一个有效文件的测试用例中,我得到了400 Bad Request页面。这特别奇怪,因为在algorithm()函数中,每个if / else语句节中都有print语句。但是,这是终端输出:1)不提供名称,2)不提供文件,然后3)提供正确的名称和文件:

Opened database successfully
Table created successfully
>>>> Opened database successfully <<<<
>>>> Table Dropped
>>>> Table created successfully <<<<
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [15/Jan/2019 14:12:07] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [15/Jan/2019 14:12:09] "GET /algorithm HTTP/1.1" 200 -
POST method invoked
No name given
127.0.0.1 - - [15/Jan/2019 14:12:10] "POST /algorithm HTTP/1.1" 302 -
127.0.0.1 - - [15/Jan/2019 14:12:10] "GET /algorithm HTTP/1.1" 200 -
POST method invoked
No file given
127.0.0.1 - - [15/Jan/2019 14:12:16] "POST /algorithm HTTP/1.1" 302 -
127.0.0.1 - - [15/Jan/2019 14:12:16] "GET /algorithm HTTP/1.1" 200 -
POST method invoked
127.0.0.1 - - [15/Jan/2019 14:12:35] "POST /algorithm HTTP/1.1" 400 -

这表明else语句甚至都没有输入,因为没有“数据有效”打印语句,即使其他语句按预期打印也是如此。

0 个答案:

没有答案