如何使用python发送服务器响应实例

时间:2018-06-21 17:23:50

标签: python http-status-codes

我有一个尝试注册新用户的函数,最后我希望该函数返回服务器的响应实例(如200),该怎么做? 这是我的功能...

@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""

    # forget any previous session

    session.clear()

    if request.method == "POST":

        if not request.form.get("username"):
            return apology("Enter username", 400)

        if not request.form.get("password"):
            return apology("Enter a password", 400)

        if not request.form.get("confirmation"):
            return apology("Enter the password again", 400)

        if not request.form.get("password") == request.form.get("confirmation"):
            return apology("Passwords do not match", 400)

        rows = db.execute("SELECT username FROM users WHERE username = :username", username = request.form.get("username"))

        if len(rows) > 0:
            return apology("Username already in use", 400)

        # inserting data to database

        rows = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hashh)",
                            username = request.form.get("username"), hashh = generate_password_hash(request.form.get("password")))

        session["user_id"] = rows

        return 200

    else:

        return render_template("register.html")

0 个答案:

没有答案