7

我有一个以前可以使用的带有烧瓶的应用程序,但是现在我在其中使用了蓝图并尝试运行它,但出现了错误,所以我想知道这是 g.user 无法正常工作的蓝图问题吗?以及如何修复它Thnx :)

应用程序/布局/__ 初始化 __.py :

from flask import Blueprint
layout = Blueprint('layout', __name__)
from . import view

__ 初始化 __ .py

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
import psycopg2
from config import basedir
from config import config

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy()
lm = LoginManager()
lm.init_app(app)
lm.login_view = 'login'


def create_app(config_name):
    app = Flask(__name__)
    app.config['DEBUG'] = True
    app.config.from_object(config[config_name])
    db.init_app(app)
    from .layout import layout as appr_blueprint
    # register our blueprints
    app.register_blueprint(appr_blueprint)


return app

视图.py:

@layout.before_request 
      def before_request():
         g.user = current_user 
    @layout.route('/login', methods = ['GET', 'POST']) 
      def login():
            form = LoginForm()
            #checks if the user is authernticated
            #or not, if yes it skips authentfic.
            if current_user is not None and current_user.is_authenticated():
                   return redirect(url_for('user'))
         #does not allow user to use get method
            if request.method == 'GET':
                  return render_template('login.html',
                            form = form,
                          title = 'Login')
        #taking the user submitted data and checking if it exists in the database
           user_in_db = User.query.filter_by(name=form.name.data.lower()).first()

        #if the username is not wrong
            if user_in_db is not None and user_in_db != False:
                    if form.email.data !=  user_in_db.email:
                           flash('Email is incorrect')
                            return redirect(url_for('login'))
                    login_user(user_in_db)
                    return redirect(url_for('user',page=1,sortby='normal'))
           else:
               flash('Username does not exists')
                 return render_template('login.html',
                        form = form,
                       title = 'Login')

base.html:

<div id="bodyAll">
        <div class="navbar navbar-inverse navbar-fixed-top">

            <div class="container-fluid">
                <ul class="nav navbar-nav">
                    <li id="logo">
                        <a href="{{ url_for('layout.home') }}">
                            <span id="globe"class="glyphicon glyphicon-home"></span>
                            Home
                        </a>
                    </li>
                    <li id="logo">
                        <a href="{{ url_for('layout.new') }}">
                            <span id="globe"class="glyphicon glyphicon-plus"></span>
                            Add Monkey
                        </a>
                    </li>
                    <li>
                        <a href="{{ url_for('layout.user',page = '1', sort = 'normal', monkey = monkey) }}">
                            <span class="glyphicon glyphicon-tree-deciduous"></span>
                            Jungle
                        </a>
                    </li>

                {% if current_user.is_authenticated() %} //**Got error here**
                    <li>
                        <a href="#">
                            <span class="glyphicon glyphicon-user"></span>
                            {{g.user.name.capitalize()}}
                        </a>
                    </li>
                    {% endif %}
4

2 回答 2

8

你应该在init .py中注册 login_manager尝试添加这个

login_manager.init_app(app)
于 2017-03-28T15:48:32.337 回答
3

您添加 LoginManager init .py 或 app.py的地方

login_manager = LoginManager()
 #Added this line fixed the issue.
login_manager.init_app(app) 
login_manager.login_view = 'users.login'
于 2019-04-11T17:56:14.300 回答