Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @app.route("/register", methods=["GET","POST"])
- def register():
- # forget any user_id
- session.clear()
- # if user reached route via POST (as by submitting a form via POST)
- if request.method == "POST":
- # ensure username was submitted
- if not request.form.get("username"):
- return apology("must provide username")
- # ensure password was submitted
- if not request.form.get("password"):
- return apology("must provide password")
- #ensure again password submitted
- if not request.form.get("again_password"):
- return apology("must provide password again")
- #ensure both password match
- if request.form.get("password") == request.form.get("again_password"):
- #query database for username
- rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
- #Ensure usename don't exist already
- if len(rows) == 0:
- #Encrypt password
- h = pwd_context.encrypt(request.form.get("password"))
- #insert username and password to SQL data base
- key = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username=request.form.get("username"), hash=h)
- #Remember user id after successful register.
- session["user_id"] = key [0] ["id"]
- # redirect user to home page
- return redirect(url_for("index"))
- else:
- #return apology if username already exist
- return apology("Username already exist")
- else:
- #return apology if both password don't match with each other
- return apology("both password should match")
- #Return to register.html and use post mathod
- else:
- return render_template("register.html")
Add Comment
Please, Sign In to add comment