如何将Flask Login与蓝图一起使用 [英] How to use Flask Login with Blueprints
本文介绍了如何将Flask Login与蓝图一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对我的烧瓶应用程序使用LoginManager.当我运行该应用程序并打开索引页面时,它显示以下内容
wsgi_app中的 File``... \ python38-32 \ lib \ site-packages \ flask \ app.py''行2447响应= self.full_dispatch_request()在full_dispatch_request中,文件1955行的文件``... \ python38-32 \ lib \ site-packages \ flask \ app.py''rv = self.handle_user_exception(e)在handle_user_exception中的第1821行中的文件"... \ python38-32 \ lib \ site-packages \ flask \ app.py"加价(exc_type,exc_value,tb)重新列出文件"... \ python38-32 \ lib \ site-packages \ flask \ _compat.py",第39行提高价值在full_dispatch_request中,在第1950行中的文件``... \ python38-32 \ lib \ site-packages \ flask \ app.py''rv = self.dispatch_request()在dispatch_request中,文件"... \ python38-32 \ lib \ site-packages \ flask \ app.py",第1936行返回self.view_functions [rule.endpoint](** req.view_args)文件"... \ root \ project \ user_auth \ routes.py",第18行,在索引中返回render_template('index.html',title ='Welcome')在render_template中,文件"... \ python38-32 \ lib \ site-packages \ flask \ templating.py",第137行返回_render(_render中的文件"... \ python38-32 \ lib \ site-packages \ flask \ templating.py",第120行rv = template.render(上下文)渲染中的文件"... \ python38-32 \ lib \ site-packages \ jinja2 \ environment.py",行1090self.environment.handle_exception()在handle_exception中的文件"... \ python38-32 \ lib \ site-packages \ jinja2 \ environment.py",第832行重新提高(* rewrite_traceback_stack(source = source))重新列出文件"... \ python38-32 \ lib \ site-packages \ jinja2 \ _compat.py",第28行提高价值.with_traceback(tb)顶级模板代码中的文件"... \ root \ project \ user_auth \ templates \ index.html",第1行{%扩展了"base.html"%}顶级模板代码中的文件"... \ root \ project \ user_auth \ templates \ base.html",第13行{%,如果current_user.is_anonymous%}getattr中的文件``... \ python38-32 \ lib \ site-packages \ jinja2 \ environment.py'',第471行返回getattr(obj,attribute)jinja2.exceptions.UndefinedError:'current_user'未定义
这是我的结构
root/config.py|项目/|__init__.py|user_auth/|__init__.py|app.py|route.py
在app/ init .py文件中,该文件如下
从烧瓶导入烧瓶的 从配置导入配置app = Flask(__ name__)app.config.from_object(Config)从.user_auth.routes导入user_auth_bpapp.register_blueprint(user_auth_bp,url_prefix =".)
来自user_auth init ..py文件为
从烧瓶导入烧瓶的 从配置导入配置从flask_sqlalchemy导入SQLAlchemy从flask_migrate导入迁移从flask_login导入LoginManager从烧瓶导入current_appapp = Flask(__ name__)app.config.from_object(Config)db = SQLAlchemy(应用程序)迁移=迁移(app,db)login = LoginManager(app)login.login_view ='.login'app.config ['SECRET_KEY'] ='试用版'从..user_auth导入路线,模型
这是user_auth/routes.py文件
来自..user_auth导入应用程序,数据库的 从flask_login导入login_required从..user_auth.forms导入RegistrationForm,LoginForm从flask导入请求,render_template,flash,重定向,url_for,蓝图从werkzeug.urls导入url_parse从flask_login导入logout_user从flask_login导入current_user,login_user从..user_auth.models导入用户从烧瓶进口蓝图user_auth_bp =蓝图('user_auth',__name__,template_folder ='templates')@ user_auth_bp.route('/')@ user_auth_bp.route('/index')def index():返回render_template('index.html',title ='Welcome')@ user_auth_bp.route('/register',methods = ["POST","GET"])def register():表格= RegistrationForm()如果form.validate_on_submit():打印(已验证")#创建用户模型用户名= form.username.data密码= form.password.data电子邮件= form.email.datanewUser =用户(用户名=用户名,电子邮件=电子邮件)newUser.set_password(密码)db.session.add(newUser)db.session.commit()返回重定向(url_for('.index'))别的:返回render_template('register.html',title ='Welcome',form = form)...(其他路径)
我正在运行Windows,并使用了以下命令
set FLASK_APP = project
然后我使用
烧瓶运行
预先感谢您的帮助.
解决方案
您可以尝试搜索相同的问题
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.
解决方案
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
这篇关于如何将Flask Login与蓝图一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文