Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from flask import Flask, render_template, url_for, flash, redirect from forms import RegistrationForm, LoginForm from werkzeug.security import generate_password_hash, check_password_hash import shelve app = Flask(__name__, template_folder='template') app.app_context().push()

from flask import Flask, render_template, url_for, flash, redirect from forms import RegistrationForm, LoginForm from werkzeug.security import generate_password_hash, check_password_hash import shelve app = Flask(__name__, template_folder='template') app.app_context().push() app.config['SECRET_KEY'] = 'HELLO WORLD!' db = shelve.open("site.db", writeback=True) class User(object): def __init__(self, username, email, password): self.username = username self.email = email self.password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password) @app.route("/home") def home(): return render_template('home.html') @app.route("/") def index(): return redirect(url_for('login')) @app.route("/register", methods=['GET', 'POST']) def register(): form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data, password=generate_password_hash(form.password.data)) if form.email.data in db: flash('Email already exists.', 'danger') return redirect(url_for('register')) db[form.email.data] = user flash('Your account has been created! You are now able to log in', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form) @app.route("/login", methods=['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): if form.email.data in db: user = db[form.email.data] if user.check_password(form.password.data): flash('You have been logged in!', 'success') return redirect(url_for('home')) else: flash('Incorrect password. Please try again.', 'danger') return redirect(url_for('login')) else: flash('This email is not registered. Please register first.', 'danger') return redirect(url_for('register')) return render_template('login.html', title='Login', form=form) @app.route("/delete_user/") def delete_user(email): del db[email] flash('User deleted successfully!', 'success') return redirect(url_for('home')) if __name__ == '__main__': app.run(debug=True) db.close()

his is my python so basically i want to do register a user by using registration form and using that created user to login if the login is sucessful it will redirect to the home page now my problem is when i tried to login using using the correct credientials it still stuck in login and does not enter to the home and when i tried manually /home in the web page it works i have already specified the home page accordingly because when i tried /login , /register and /home it works only would you help troubleshoot the problem and give the solution

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions