[python] Register Function
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- @app.route("/register", methods=["GET", "POST"])
- def register():
- """Register user"""
- # User reached route via POST (as by submitting a form via POST)
- if request.method == "POST":
- # make sure user types a username
- if not request.form.get("username"):
- flash("You must provide a username")
- return render_template("register.html")
- # make sure user types a password
- elif not request.form.get("password"):
- flash("You must provide a password")
- return render_template("register.html")
- # make sure user types a confirmation
- elif not request.form.get("confirmation"):
- flash("You must confirm your password")
- return render_template("register.html")
- # make sure user confirms password
- elif request.form.get("password") != request.form.get("confirmation"):
- flash("Confirmation doesn't match password")
- return render_template("register.html")
- # hash password
- hashed = generate_password_hash(request.form.get("password"))
- # Insert username and hash of password in the database
- result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)",
- username=request.form.get("username"), hash=hashed)
- # Ensure username is not repeated
- if not result:
- flash("username unavailable")
- return render_template("register.html")
- # Start session
- rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
- session["user_id"] = rows[0]["id"]
- # redirect user to home
- return redirect("/")
- # if user reached route via GET
- else:
- return render_template("register.html")
Editor
You can edit this paste and save as new: