ahmedraza

register final

Feb 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 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.                
  25.                 db.execute("INSERT INTO users (username, hash) VALUES(:username, :password)", username=request.form["username"], password=pwd_context.encrypt("password"))
  26.                 #return to login.html page to login after successful register
  27.                 return render_template("login.html")
  28.             else:
  29.                 #return apology if username already exist
  30.                 return apology("Username already exist")
  31.         else:
  32.             #return apology if both password don't match with each other
  33.             return apology("both password should match")
  34.     #Return to register.html and use post mathod        
  35.     else:
  36.         return render_template("register.html")
Add Comment
Please, Sign In to add comment