Flask中相同路由中的不同类型的POST请求

时间:2017-04-10 21:50:00

标签: python mysql post flask

我想在我的网站上有一个页面,你有多个按钮发送不同的POST请求来修改我的数据库的某些部分。

目前,只执行top if语句。如果我尝试两个底部的,我得到:浏览器(或代理)发送了一个该服务器无法理解的请求。

如果我切换它们,它总是最后的if语句被执行。

我这样做错了吗?有没有更好的方法来做这种事情?

@app.route('/', methods=["GET", "POST"])
@login_required
def homepage():
    if request.method == "POST" and request.form['estimatedCost']:
        _projectName = request.form['projectName']
        _estimatedCost = request.form['estimatedCost']
        _amountAllocated = request.form['amountAllocated']
        conn, cursor = connectDB()
        cursor.execute("INSERT INTO `project` (`name`, `estimatedCost`, `amountAllocated`, `pStatus`, `actualCost`, `estimatedDuration`, `actualDuration`, `costDifference`) VALUES ( '" + _projectName + "', '" + _estimatedCost + "', '" + _amountAllocated + "', 'NOT STARTED', 0, 0, 0, NULL)")
        conn.commit()
        conn.close()
        return redirect('/')
    if request.method == "POST" and request.form['delete']:
        _delete = request.form['delete']
        conn, cursor = connectDB()
        cursor.execute("DELETE FROM project WHERE name = '" + _delete + "'")
        conn.commit()
        conn.close()
        return redirect('/')
    if request.method == "POST" and request.form['pid']:
        _pid = request.form['pid']
        _status = request.form['status']
        conn, cursor = connectDB()
        cursor.execute("UPDATE project SET pStatus = '" + _status + "' WHERE name = '" + _pid + "'")
        conn.commit()
        conn.close()
        return redirect('/')
    conn, cursor = connectDB()
    cursor.execute("SELECT * FROM project")
    projects = cursor.fetchall()
    conn.close()
    return render_template("dashboard.html", projectDic = projects)

1 个答案:

答案 0 :(得分:3)

我设法为我的问题找到了解决方案。

因为request.form['key']导致错误,如果它不存在,而不是只是变为False,它会使页面崩溃。

相反,我使用"key" in request.form来检查表单中是否填写了该输入。

以下是更正后的代码:

@app.route('/', methods=["GET", "POST"])
@login_required
def homepage():
    if request.method == "POST" and "estimatedCost" in request.form:
        _projectName = request.form['projectName']
        _estimatedCost = request.form['estimatedCost']
        _amountAllocated = request.form['amountAllocated']
        conn, cursor = connectDB()
        cursor.execute("INSERT INTO `project` (`name`, `estimatedCost`, `amountAllocated`, `pStatus`, `actualCost`, `estimatedDuration`, `actualDuration`, `costDifference`) VALUES ( '" + _projectName + "', '" + _estimatedCost + "', '" + _amountAllocated + "', 'NOT STARTED', 0, 0, 0, NULL)")
        conn.commit()
        conn.close()
        return redirect('/')
    if request.method == "POST" and "delete" in request.form:
        _delete = request.form['delete']
        conn, cursor = connectDB()
        cursor.execute("DELETE FROM project WHERE name = '" + _delete + "'")
        conn.commit()
        conn.close()
        return redirect('/')
    if request.method == "POST" and "pid" in request.form:
        _pid = request.form['pid']
        _status = request.form['status']
        conn, cursor = connectDB()
        cursor.execute("UPDATE project SET pStatus = '" + _status + "' WHERE name = '" + _pid + "'")
        conn.commit()
        conn.close()
        return redirect('/')
    conn, cursor = connectDB()
    cursor.execute("SELECT * FROM project")
    projects = cursor.fetchall()
    conn.close()
    return render_template("dashboard.html", projectDic = projects)