main.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import pymysql
  2. from app import app
  3. from db_config import mysql
  4. from flask import jsonify, request, render_template
  5. @app.route('/user_check', methods=['POST'])
  6. def username_check():
  7. conn = None
  8. cursor = None
  9. try:
  10. username = request.form['username']
  11. # validate the received values
  12. if username and request.method == 'POST':
  13. conn = mysql.connect()
  14. cursor = conn.cursor(pymysql.cursors.DictCursor)
  15. cursor.execute("SELECT * FROM user WHERE login_username=%s", username)
  16. row = cursor.fetchone()
  17. if row:
  18. resp = jsonify('<span style=\'color:red;\'>Username unavailable</span>')
  19. resp.status_code = 200
  20. return resp
  21. else:
  22. resp = jsonify('<span style=\'color:green;\'>Username available</span>')
  23. resp.status_code = 200
  24. return resp
  25. else:
  26. resp = jsonify('<span style=\'color:red;\'>Username is required field.</span>')
  27. resp.status_code = 200
  28. return resp
  29. except Exception as e:
  30. print(e)
  31. finally:
  32. cursor.close()
  33. conn.close()
  34. @app.route('/')
  35. def home():
  36. return render_template('username.html')
  37. if __name__ == "__main__":
  38. app.run()