Can't connect route from one blueprint to another

Viewed 34

I am trying to route the user after logging in to their dashboard which is stored in another blueprint but I keep running into the same issue where it cannot find the required route.

@auth.route('/login', methods=['POST'])
def post_login():
    username = request.form['username']
    password = request.form['password']
    remember = request.form.get('remember')

    existing_user = Users.query.filter_by(username=username).first() 
    pw_hash = bcrypt.generate_password_hash(password)
    if not existing_user or not (existing_user.password, pw_hash):
        flash('Please check your login details and try again. If you do not have an account')
        return redirect(url_for('auth.get_login'))

    login_user(existing_user, remember=remember)
    return redirect(url_for(user.dashboard))

The route I want:

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

File structure:

├── admin
│   ├── admin.py
│   └── templates
│       └── admin
├── app.py
├── auth
│   ├── auth.py
│   └── templates
│       └── auth
│           ├── forgot_password.html
│           ├── login.html
│           └── register.html
├── config.py
├── forms.py
├── general
├── models.py
├── static
│   └── favicon.ico
└── user
    ├── templates
    │   └── user
    │       └── dashboard.html
    └── user.py

Error

AttributeError: 'Blueprint' object has no attribute 'dashboard'
1 Answers

This is the line that causes the issue:

return redirect(url_for(user.dashboard))

Try to redirect directly to "dashboard":

return redirect(url_for("user.dashboard"))
Related