Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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-32libsite-packagesflaskapp.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "...python38-32libsite-packagesflaskapp.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "...python38-32libsite-packagesflaskapp.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "...python38-32libsite-packagesflask\_compat.py", line 39, in reraise
    raise value
  File "...python38-32libsite-packagesflaskapp.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "...python38-32libsite-packagesflaskapp.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "...
ootprojectuser_auth
outes.py", line 18, in index
    return render_template('index.html', title='Welcome')
  File "...python38-32libsite-packagesflask	emplating.py", line 137, in render_template
    return _render(
  File "...python38-32libsite-packagesflask	emplating.py", line 120, in _render
    rv = template.render(context)
  File "...python38-32libsite-packagesjinja2environment.py", line 1090, in render
    self.environment.handle_exception()
  File "...python38-32libsite-packagesjinja2environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "...python38-32libsite-packagesjinja2\_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "...
ootprojectuser_auth	emplatesindex.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "...
ootprojectuser_auth	emplatesase.html", line 13, in top-level template code
    {% if current_user.is_anonymous %}
  File "...python38-32libsite-packagesjinja2environment.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
272 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...