Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, request, render_template
- import string
- app = Flask(__name__)
- app.config['DEBUG'] = True
- @app.route('/')
- def display_form():
- return render_template('index.html')
- @app.route('/', methods=['POST'])
- def validate_form():
- username = request.form['username']
- password = request.form['password']
- verified_password = request.form['verify']
- email = request.form['email']
- invalidChars = set(' ')
- validChars = set('@, .')
- username_content = ""
- email_content = ""
- errors_present = False
- # Here we will check the validity of the username. It cannot contain spaces, be empty, be less than 3 character or longer than 20 characters.
- if any(char in invalidChars for char in username) or len(username)<3 or len(username)>20:
- username_error_msg = "That is not a valid username"
- username_content = ""
- errors_present = True
- else:
- username_error_msg = ""
- username_content = username
- # Here we validate the password. It follows the same rules as the username.
- if any(char in invalidChars for char in password) or len(password)<3 or len(password)>20:
- password_error_msg = "That is not a valid password"
- errors_present = True
- else:
- password_error_msg = ""
- # Here we validate the 'verify password' field. If it does not match the passowrd it throws an error
- ver_pass_err_stat = verified_password != password
- if ver_pass_err_stat == True:
- verify_error_msg = "Passwords do not match"
- errors_present = True
- else:
- verify_error_msg = ""
- # Here we validate the email. It can be left blank but if it does contain characters we check to see if it is a valid email by looking for the @ symbol and '.' character
- if email != "":
- if any(char not in validChar for char in email):
- email_error_msg = "That is not a valid email address"
- email_content = ""
- errors_present = True
- else:
- email_error_msg = ""
- email_content = email
- else:
- email_error_msg = ""
- email_content = email
- # If there are any errors present it will rerender the form with a blanket display of any present errors. If there are no errors present then it will render the 'Welcome' template instead.
- if errors_present == True:
- return render_template('index.html',
- username = username_content,
- username_error = username_error_msg,
- password_error = password_error_msg,
- verify_error = verify_error_msg,
- email = email_content,
- email_error = email_error_msg
- )
- else:
- render_template('welcome.html', new_user = username)
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement