0

I'm trying to use LoginManager for my flask application. When I run the application and open the index page, it says the following

File "...\python38-32\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "...\python38-32\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "...\python38-32\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "...\python38-32\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "...\python38-32\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "...\python38-32\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "...\root\project\user_auth\routes.py", line 18, in index
    return render_template('index.html', title='Welcome')
  File "...\python38-32\lib\site-packages\flask\templating.py", line 137, in render_template
    return _render(
  File "...\python38-32\lib\site-packages\flask\templating.py", line 120, in _render
    rv = template.render(context)
  File "...\python38-32\lib\site-packages\jinja2\environment.py", line 1090, in render
    self.environment.handle_exception()
  File "...\python38-32\lib\site-packages\jinja2\environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "...\python38-32\lib\site-packages\jinja2\_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "...\root\project\user_auth\templates\index.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "...\root\project\user_auth\templates\base.html", line 13, in top-level template code
    {% if current_user.is_anonymous %}
  File "...\python38-32\lib\site-packages\jinja2\environment.py", line 471, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'current_user' is undefined

Here is my structure

root /
config.py
   |project/
        | __init__.py
        | user_auth/

              | __init__.py
              | app.py
              | routes.py

In the app/init.py file, it is the following

from flask import Flask
from config import Config

app = Flask(__name__)
app.config.from_object(Config)

from .user_auth.routes import user_auth_bp
app.register_blueprint(user_auth_bp, url_prefix="")

From the user_auth init.py file is

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask import current_app

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = '.login'

app.config['SECRET_KEY'] = 'trial'

from ..user_auth import routes, models

This is the user_auth/routes.py file

from ..user_auth import app, db
from flask_login import login_required
from ..user_auth.forms import RegistrationForm, LoginForm
from flask import request, render_template, flash, redirect, url_for, Blueprint
from werkzeug.urls import url_parse
from flask_login import logout_user

from flask_login import current_user, login_user
from ..user_auth.models import User


from flask import Blueprint
user_auth_bp = Blueprint('user_auth', __name__, template_folder='templates')

@user_auth_bp.route('/')
@user_auth_bp.route('/index')
def index():
    return render_template('index.html', title='Welcome')


@user_auth_bp.route('/register', methods=["POST", "GET"])
def register():

    form = RegistrationForm()

    if form.validate_on_submit():
        print("validated")

        # Create User Model
        username = form.username.data
        password = form.password.data
        email = form.email.data

        newUser = User(username=username,email=email)
        newUser.set_password(password)

        db.session.add(newUser)
        db.session.commit()
        
        return redirect(url_for('.index'))
    else: 

        return render_template('register.html', title='Welcome', form=form)

... (Other paths)

I am running Windows and used the following command

set FLASK_APP=project

I then use

flask run

Thank you in advance for the help.

Daniel
  • 29
  • 3
  • 1
    As I said in the other post, Why do you have 2 flasks app ? – azro Nov 08 '20 at 10:00
  • I'm not sure, I've been following different tutorials. Also, I created an app, then retrospectively tried to create a blueprint – Daniel Nov 08 '20 at 10:33
  • How should I change the files to fix it? – Daniel Nov 08 '20 at 10:38
  • There should be only one Flask app, the one you run the other one shouldn't exists – azro Nov 08 '20 at 11:57
  • Do you mean that in the init files, the app=Flask(__name__) only needs to be in the project/__init__.py? Do I move everything from the user_auth/init to the project/init – Daniel Nov 08 '20 at 12:08
  • No, if you move it you'll still have 2 apps, you shouldn't – azro Nov 08 '20 at 13:08

1 Answers1

0

You can try to search same problem UndefinedError: 'current_user' is undefined

Check the init file:

login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)
    from .models import User
    @login_manager.user_loader
    def load_user(user_id):
        user = User.query.filter_by(id=user_id).first()

And strure, i think you should do like that My Strure