Guest User

register3

a guest
Feb 15th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. @app.route("/register", methods=["GET","POST"])
  2. def register():
  3.     # forget any user_id
  4.     session.clear()
  5.     # if user reached route via POST (as by submitting a form via POST)
  6.     if request.method == "POST":
  7.                # ensure username was submitted
  8.         if not request.form.get("username"):
  9.             return apology("must provide username")
  10.            
  11.         # ensure password was submitted
  12.         if not request.form.get("password"):
  13.             return apology("must provide password")
  14.         #ensure again password submitted
  15.         if not request.form.get("again_password"):
  16.             return apology("must provide password again")
  17.         #ensure both password match    
  18.         if request.form.get("password") == request.form.get("again_password"):
  19.             #query database for username
  20.             rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
  21.            #Ensure usename don't exist already
  22.             if  len(rows) == 0:    
  23.                 #insert username and password to SQL data base
  24.                 db.execute("INSERT INTO users (username, password) VALUES(:username, :password)", username=request.form["username"], password=request.form["password"])
  25.                 #return to login.html page to login after successful register
  26.                 return render_template("login.html")
  27.             else:
  28.                 #return apology if username already exist
  29.                 return apology("Username already exist")
  30.         else:
  31.             #return apology if both password don't match with each other
  32.             return apology("both password should match")
  33.     #Return to register.html and use post mathod        
  34.     else:
  35.         return render_template("register.html")
Add Comment
Please, Sign In to add comment