在我的应用中,我正在使用Flask login应用。但我无法将其配置为使用自定义视图。
我的项目结构:
ProjectName
-app
--auth
----models.py
----views.py
app_settings.py
config.py
extensions.py
app_settings.py:
# -*- coding: utf-8 -*-
import flask
from werkzeug.routing import BaseConverter
from app.auth.views import auth
from extension import db, login_manager
from config import BaseConfig
from app.auth.models import User, Role, load_user
# For import *
__all__ = ['create_app']
DEFAULT_BLUEPRINTS = (
main,
auth
)
def create_app(config=None, app_name=None, blueprints=None):
if blueprints is None:
blueprints = DEFAULT_BLUEPRINTS
app = flask.Flask('app', instance_relative_config=True)
app.config.from_object(BaseConfig)
configure_blueprints(app, blueprints)
configure_extensions(app)
configure_hook(app)
configure_error_handlers(app)
return app
def configure_extensions(app):
db.init_app(app)
# Flask-Login
# https://flask-login.readthedocs.org/en/latest/
login_manager.init_app(app)
login_manager.user_loader(load_user)
login_manager.login_view = "/auth/login"
@app.teardown_request
def shutdown_session(exception=None):
from extension import db
db.session.remove()
我试过了:login_manager.login_view = "auth.login"
。但没有任何帮助。它继续使用标准登录视图和模板。
AUTH / views.py
from flask.ext.login import login_user
auth = flask.Blueprint('auth', __name__, url_prefix='/auth')
@auth.route('/login', methods=['GET', 'POST'])
def login():
# Here we use a class of some kind to represent and validate our
# client-side form data. For example, WTForms is a library that will
# handle this for us, and we use a custom LoginForm to validate.
form = LoginForm(flask.request.form)
if form.validate_on_submit():
# Login and validate the user.
# user should be an instance of your `User` class
user = User.query.filter_by(email=form.email.data).first()
login_user(user)
flask.flash('Logged in successfully.')
next = flask.request.args.get('next')
# next_is_valid should check if the user has valid
# permission to access the `next` url
return flask.redirect(flask.request.args.get('next', '/'))
return flask.render_template('auth/login.html', form=form)
答案 0 :(得分:0)
尝试login_manager.login_view = "login"
您将要将其指向登录视图的URL。在这种情况下,由@auth.route('/login')
只需要端点login
。