def register(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): try: hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash( f'Your have successfully registered. Please confirm your email to login', 'success') send_confirmation_email(user) return redirect(url_for('users.login')) except Exception as e: db.session.rollback() flash( f'Registration is unsuccessful due to some server error. Try Again', 'Danger') return render_template('register.html', title="Register", form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() # to check whether form submitted correctly or not if form.validate_on_submit(): # hashing the password for security purpose hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') # creating a new user user = User(username=form.username.data, email=form.email.data, password=hashed_password) # adding new user to the database db.session.add(user) db.session.commit() # flash is one t@userspop up message # writing f in flash message bcoz it is a flash string--> syntax it is (f strings used in python 3.6 and above) # passing success as second argument just a bootstrap class(that we want flash message to have) bcoz flash accepts ALERT as 2nd argument flash(f'Account created for {form.username.data}!', 'success') # redirecting user to home page after successful submission to avoid any confusion for the user # url_for (name of the function where have to redirect and not the route name) return redirect(url_for('users.login')) return render_template('register.html', title='Register', form=form)
def about(): form = RegistrationForm() return render_template('about.html', title='About', form = form)