Python Forum
[Flask]After login page is not redirecting me to dashboard
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Flask]After login page is not redirecting me to dashboard
#1
Hi . As title says after I press login button the page refreshes and it doesn't redirect me to the '/dashboard' route .
My code :
from flask import Flask, render_template, redirect, request, url_for 
from flask_wtf import FlaskForm
from wtforms.validators import InputRequired, Email, Length
from wtforms import Form, BooleanField, StringField, PasswordField, validators
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user


app = Flask(__name__)
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100))
    email = db.Column(db.String(100))
    password = db.Column(db.String(100))
    rank = db.Column(db.String(100), default='Newbie')

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


class RegisterFormValidation(FlaskForm):
    email = StringField('email', validators=[InputRequired(), Email(message='Invalid email'), Length(max=50)])
    username = StringField('username', validators=[InputRequired(), Length(min=2, max=15)])
    password = PasswordField('password', validators=[InputRequired(), Length(min=2, max=80)])
    
class LoginForm(FlaskForm):
    username = StringField('username', validators=[InputRequired(), Length(min=4, max=15)])
    password = PasswordField('password', validators=[InputRequired(), Length(min=8, max=80)])
    

@app.route('/')
def index():  
    return render_template('index.html')


@app.route('/register', methods=['GET','POST'])
def register():
    
    if RegisterFormValidation().validate_on_submit():
        new_user = User(username=RegisterFormValidation().username.data, email=RegisterFormValidation().email.data, password=RegisterFormValidation().password.data)
        db.session.add(new_user)
        db.session.commit()
    return render_template('register.html', form=RegisterFormValidation())



@app.route('/login', methods=['GET','POST'])
def login():
    
    if LoginForm().validate_on_submit():
        user = User.query.filter_by(username=LoginForm().username.data).first()
        if user.password == LoginForm().password.data:
            login_user(user)
            return redirect(url_for('dashboard'))
     
    return render_template('login.html', form=LoginForm())

@app.route('/dashboard')
@login_required
def dashboard():
    return render_template('dashboard.html', name=current_user.username)

@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('index'))

@app.route('/delete/<int:id>')
@login_required
def delete(id):
    userid = User.query.get(id)
    db.session.delete(userid)
    db.session.commit()
    return render_template('dashboard.html')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reload flask current page GrahamL 2 125 Jan-08-2021, 08:31 AM
Last Post: GrahamL
  API auto-refresh on HTML page using Flask toc 2 312 Dec-23-2020, 02:00 PM
Last Post: toc
  Flask login help dangermaus33 0 171 Dec-07-2020, 04:06 PM
Last Post: dangermaus33
  Download a link that re-directs to a login page justanotherpythonnoob 1 277 Oct-23-2020, 03:27 PM
Last Post: Aspire2Inspire
  Auto-Updating Dashboard w/ Notifications Kadin 1 222 Sep-28-2020, 12:17 PM
Last Post: DeaD_EyE
  Flask - adding new page affects all other pages CMR 15 1,045 Mar-28-2020, 04:13 PM
Last Post: CMR
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 847 Mar-19-2020, 06:13 PM
Last Post: apollo
  Redirecting all Flask logs georgelza 1 1,465 Nov-21-2018, 01:52 PM
Last Post: thomasp
  Python /Flask Login with LDAP Auth pythonnubie 4 5,185 Apr-16-2018, 03:14 PM
Last Post: nilamo
  Flask - Opening second page via href is failing - This site can’t be reached rafiPython1 2 2,083 Apr-11-2018, 08:41 AM
Last Post: rafiPython1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020