【问题标题】:Route two modules to the same URL in Flask by session通过会话将两个模块路由到 Flask 中的相同 URL
【发布时间】:2013-01-25 10:50:13
【问题描述】:

我有一个未登录的模块/蓝图 welcome 和一个已登录的蓝图 home。我希望具有有效会话的用户转到home.index,并希望作为客人的用户转到welcome.index。但是,我遇到了问题,因为这两个函数都路由到同一个 URL,/。

我怎样才能使这个功能成为可能?我已经尝试添加:

if(logged_in():
    redirect(url_for('home.index'))

到欢迎蓝图中的index(),但这当然只会导致循环重定向,因为home.index 的URL 与welcome.index 相同。

我还尝试仅在 logged_in() 为真时定义 welcome.index。但是,这会导致问题,因为站点上还有其他链接指向 welcome.index,如果用户登录,这些会导致错误,因为 welcome.index 在技术上不再存在。

编辑

我从这段代码中看到了这个错误AttributeError: 'Blueprint' object has no attribute 'index':

from flask import Flask, session, g

from modules.welcome import welcome
from modules.home import home as home

from modules.home import index
from modules.welcome import index

 app = Flask(__name__)
 app.config.from_pyfile('config.cfg')

 app.register_blueprint(welcome)
 app.register_blueprint(home)

 @app.route('/', methods=['GET', 'POST'])
 def index():
    if 'user_id' in session:
        return home.index()
    else:
        return welcome.index()

编辑 #2:蓝图代码

modules/home.py 中的代码:

from flask import Blueprint, render_template, redirect, url_for, request, session, g

from models.User import User
from helpers.login import *

home = Blueprint('home', __name__)

def index():
    return render_template('home/index.html')

modules/welcome.py 中的代码:

from flask import Blueprint, render_template, redirect, url_for, request, session, g
import md5

from models.User import User
from helpers.login import *

welcome = Blueprint('welcome', __name__)

def index():
    alert, email = None, None
    if request.method == 'POST' and not logged_in():
        email = request.form['email'] 
        password_salt = md5.new(request.form['password']).hexdigest()
        user = User.query.filter_by(email=email , password_salt=password_salt).first()
        if user is None:
            alert = "Wrong username or password!"
        else:
            session['user_id'] = user.id
            return redirect(url_for('home.index'))
    return render_template('welcome/index.html', alert=alert, email=email)

@welcome.route('/about')
def about():
    return render_template('welcome/about.html')

@welcome.route('/tandp')
def tandp():
    return render_template('welcome/tandp.html')

@welcome.route('/logout')
def logout():
    session.pop('user_id', None)
    return redirect(url_for('welcome.index'))

@welcome.route('/register')
def register():
    error = None
    return "HI"

【问题讨论】:

    标签: python routing flask werkzeug


    【解决方案1】:

    拆分您的方法,测试记录的状态,然后调用适当的函数(在每个方法上添加您需要的参数):

    from ????? import app
    from ????? import logged_in
    import home.index
    import welcome.index
    
    @app.route('/')
    def your_basic_index_view():
       if logged_in():
           return home.index()
       else:
           return welcome.index()
    

    或者对装饰器做同样的事情。您将无法使用有条件地指向两个不同视图的单一路线。

    编辑:

    尝试以下方法:

    from flask import Flask, session, g
    
    from modules.welcome import welcome
    from modules.home import home as home
    
    from modules.home import index as h_index
    from modules.welcome import index as w_index
    
    app = Flask(__name__)
    app.config.from_pyfile('config.cfg')
    
    app.register_blueprint(welcome)
    app.register_blueprint(home)
    
    @app.route('/', methods=['GET', 'POST'])
    def index():
        if 'user_id' in session:
            return h_index()
        else:
            return w_index()
    

    【讨论】:

    • 谢谢,我能够(我认为)使用from modules.home import index 等导入函数。现在,我得到'Blueprint' object has no attribute 'index',即使我在两个蓝图中都定义了索引函数.
    • 将这些函数声明为裸函数 - 不要使用 route 装饰器来装饰它们。
    • 我确实让它们成为了裸函数,我也尝试重新启动应用程序,它似乎给了我同样的错误。
    • 我怀疑你的蓝图还有很多其他的方法和路线
    • 是的,他们有。我不确定这将如何连接到错误,但我已经用每个蓝图文件的完整代码更新了我的答案。除了index(),我还没有为home写过任何其他方法,因为我刚刚开始编写应用程序。
    猜你喜欢
    • 2015-02-05
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    相关资源
    最近更新 更多