main.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import pymysql
  2. from app import app
  3. from db_config import mysql
  4. from flask import flash, render_template, request, redirect, url_for
  5. from werkzeug import generate_password_hash, check_password_hash
  6. @app.route('/register', methods=['POST'])
  7. def save_user_info():
  8. cursor = None
  9. try:
  10. name = request.form['name']
  11. dob = request.form['dob']
  12. gender = request.form['gender']
  13. password = request.form['password']
  14. phone = request.form['phone']
  15. email = request.form['email']
  16. address = request.form['address']
  17. # validate the received values
  18. if name and dob and gender and password and phone and email and address and request.method == 'POST':
  19. #do not save password as a plain text
  20. _hashed_password = generate_password_hash(password)
  21. # save user information
  22. sql = "INSERT INTO user(name, password, email, phone, gender, dob, address) VALUES(%s, %s, %s, %s, %s, %s, %s)"
  23. data = (name, _hashed_password, email, phone, gender, dob, address)
  24. conn = mysql.connect()
  25. cursor = conn.cursor()
  26. cursor.execute(sql, data)
  27. conn.commit()
  28. flash('You registered successfully!')
  29. return redirect(url_for('.home'))
  30. else:
  31. return 'Error while saving user information'
  32. except Exception as e:
  33. print(e)
  34. finally:
  35. cursor.close()
  36. conn.close()
  37. @app.route('/')
  38. def home():
  39. return render_template('multi-step-registration.html')
  40. if __name__ == "__main__":
  41. app.run()