Home > Blockchain >  LoginManager TypeError: 'NoneType' object is not callable
LoginManager TypeError: 'NoneType' object is not callable

Time:01-15

Problem

I can't use LoginManager in Flask. If I set login_view It returns 'NoneType' object is not callable.
My Files are:

Main Folder>  
    > main.py
    > website >
          > __init__.py
          > views.py
          > models.py
          > static
          > templates

In My __init__.py

from flask import Flask
from flask_login import LoginManager
from .views import views
from .models import db,User
from flask_mobility import Mobility
from os import path

DB_NAME="database.db"

def create_app():
    app=Flask(__name__)
    Mobility(app)
    
    loginmanager=LoginManager(app)
    loginmanager.login_view('auth.login')

    app.config['SECRET_KEY']='no nothing nobody'
    app.config['SQLALCHEMY_DATABASE_URI']=f'sqlite:///{DB_NAME}'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
    db.init_app(app)
    app.register_blueprint(views, url_prefix='/')
    create_database(app)
    return app

def create_database(app):
    if not path.exists('website/' DB_NAME):
        db.create_all(app=app)

In my main.py

from website import create_app

app=create_app()

if __name__=='__main__':
    app.run(host='0.0.0.0',port=80,debug=True)

The Error

Traceback (most recent call last):
  File "G:\Python Projects\flask\main.py", line 4, in <module>
    app=create_app()
  File "G:\Python Projects\flask\website\__init__.py", line 14, in create_app
    loginmanager.login_view('auth.login')
TypeError: 'NoneType' object is not callable
[Finished in 2.9s]

CodePudding user response:

I can see that there is no auth module in your project. But, you are calling login function from auth.

If you're a beginner, login_view is the view of the login page of your project. If user is not logged in, then flask tries to redirect the user to that view. So, If your project already has a login page, replace auth.login with the module and function name of your login view.

  •  Tags:  
  • Related