examples/tutorial/flaskr/auth.py
def register():
    """Register a new user.

    Validates that the username is not already taken. Hashes the
    password for security.
    """
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]
        db = get_db()
        error = None

        if not username:
            error = "Username is required."
        elif not password:
            error = "Password is required."

        if error is None:
            try:
                db.execute(
                    "INSERT INTO user (username, password) VALUES (?, ?)",
                    (username, generate_password_hash(password)),
                )
                db.commit()
            except db.IntegrityError:
                # The username was already taken, which caused the
                # commit to fail. Show a validation error.
                error = f"User {username} is already registered."
            else:
                # Success, go to the login page.
                return redirect(url_for("auth.login"))

        flash(error)

    return render_template("auth/register.html")
Register a new user.

    Validates that the username is not already taken. Hashes the
    password for security.
    
Register a new user.
Validates that the username is not already taken. Hashes the
password for security.
Register a new user . Validates that the username is not already taken . Hashes the password for security .
def register():
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]
        db = get_db()
        error = None
        if not username:
            error = "Username is required."
        elif not password:
            error = "Password is required."
        if error is None:
            try:
                db.execute(
                    "INSERT INTO user (username, password) VALUES (?, ?)",
                    (username, generate_password_hash(password)),
                )
                db.commit()
            except db.IntegrityError:
                error = f"User {username} is already registered."
            else:
                return redirect(url_for("auth.login"))
        flash(error)
    return render_template("auth/register.html")
def register ( ) : if request . method == "POST" : username = request . form [ "username" ] password = request . form [ "password" ] db = get_db ( ) error = None if not username : error = "Username is required." elif not password : error = "Password is required." if error is None : try : db . execute ( "INSERT INTO user (username, password) VALUES (?, ?)" , ( username , generate_password_hash ( password ) ) , ) db . commit ( ) except db . IntegrityError : error = f"User {username} is already registered." else : return redirect ( url_for ( "auth.login" ) ) flash ( error ) return render_template ( "auth/register.html" )
Register a new user.
Register a new user .
"""Register a new user.

    Validates that the username is not already taken. Hashes the
    password for security.
    """ # The username was already taken, which caused the # commit to fail. Show a validation error. # Success, go to the login page.