2

I'm trying to create a simple login with flask. Unfortunately, I getting the following message when I try to start the website:

@login.user_loader AttributeError: 'function' object has no attribute 'user_loader'

I have 3 files. main.py which handles routing and app start. forms.py which stores my forms and models.py. I assume the error is eighter in main.py or models.py. Attached the project structure. project structure printscreen

main.py

from flask import Flask
app = Flask(__name__)
from flask_login import LoginManager
from flask import render_template, flash, redirect, url_for, request
from flask_login import login_user, logout_user, current_user, 
login_required
import forms
import models
import os

login = LoginManager(app)  # is this here the problem?
login.login_view = 'login' # is this here the problem?


@app.route('/login', methods=['GET', 'POST'])
def login():

    if current_user.is_authenticated:
         return render_template('index.html')
    form = forms.LoginForm()
    if form.validate_on_submit():
        user = models.User()
        user = user.get_user(form.username.data)
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        print(request.values)
        next_page = url_for('index')
        return redirect(next_page)

    return render_template('login.html', title='Sign In', form=form)


if __name__ == "__main__":
    app.secret_key = os.urandom(12)
    app.run(debug=True, host='0.0.0.0', port=4000)

models.py

from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
import os
import pypyodbc
import pymssql
from main import login

class User(UserMixin):

    id = None
    username = None
    email = None
    password_hash = None
    exits = False

    dbhost = 'XX'
    dbusername = 'XX'
    dbpassword = 'XX'
    dbdatabase = 'XX'
    isUbuntu = False
    connectionstring = ''
    paramUserName = ""

    if os.name != 'nt':
        isUbuntu = True

    if isUbuntu == False:
        connectionstring = 'Driver={SQL Server};Server=localhost;Database=PMC'

    if isUbuntu == True:
        connection = pymssql.connect(dbhost, dbusername, dbpassword, dbdatabase)
    else:
        connection = pypyodbc.connect(connectionstring)


    def __repr__(self):
        return '<User {}>'.format(self.username)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def get_user(self, User):
        cursor = self.connection.cursor()
        SQLCommand = ('Select * from  [UserData] where username=(%d)')
        Values = User
        cursor.execute(SQLCommand, Values)
        user = cursor.fetchone()
        self.connection.close()
        if user is not None:
            self.id = user[0]
            self.username = user[1]
            self.email = user[2]
            self.password_hash = user[3]
            self.exits = True
            return self
        else:
            return None

    def get_userwithid(self, ID):
        cursor = self.connection.cursor()
        SQLCommand = ('Select * from  [UserData] where id=(%d)')
        Values = ID
        cursor.execute(SQLCommand, Values)
        user = cursor.fetchone()
        self.connection.close()
        if user is not None:
            self.id = user[0]
            self.username = user[1]
            self.email = user[2]
            self.password_hash = user[3]
            self.exits = True
            return self
        else:
            return None
    def check_password(self, password):
        return check_password_hash(self.password_hash, password)


@login.user_loader # this here throws the error
def load_user(id):
    user = User()
    user = user.get_userwithid(id)
    return user
1
  • according docs you must to call login_manager.init_app(app). Commented Feb 24, 2018 at 13:30

1 Answer 1

3

You're overwriting the login variable when you create the login function. You can either change the name of the variable:

loginMngr = LoginManager(app) 

Or you can change your login function name:

def loginHandler():
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

you are awesome :-) thank you very much, i didnt see that. it works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.