Python 按会话将两个模块路由到Flask中的相同URL

Python 按会话将两个模块路由到Flask中的相同URL,python,routing,flask,werkzeug,Python,Routing,Flask,Werkzeug,我有一个未登录的模块/蓝图,欢迎,还有一个登录的蓝图,主页。我希望具有有效会话的用户转到home.index,并且希望作为来宾的用户转到welcome.index。但是,我遇到了一些问题,因为这两个函数都路由到同一个URL,/ 如何使此功能成为可能?我尝试补充: if(logged_in(): redirect(url_for('home.index')) 到欢迎蓝图中的index(),但这当然只会导致循环重定向,因为home.index的URL与welcome.index相同 如果l

我有一个未登录的模块/蓝图,
欢迎
,还有一个登录的蓝图,
主页
。我希望具有有效会话的用户转到
home.index
,并且希望作为来宾的用户转到
welcome.index
。但是,我遇到了一些问题,因为这两个函数都路由到同一个URL,
/

如何使此功能成为可能?我尝试补充:

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

如果
logged\u in()
为真,我还尝试只定义
welcome.index。但是,这会导致问题,因为网站上还有其他链接链接到
welcome.index
,如果用户登录,这些链接会导致错误,因为
welcome.index
在技术上不再存在

编辑 我看到此错误
AttributeError:“Blueprint”对象没有此代码中的属性“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:蓝图代码 模块/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')
模块/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"

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

或者对装饰师也一样。您将无法使用一条有条件指向两个不同视图的管线

编辑:

请尝试以下操作:

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()

谢谢,我能够(我想)从modules.home import index等使用
导入函数。不过现在,我得到的
'Blueprint'对象没有属性“index”
,尽管我在两个蓝图中都定义了索引函数。将这些函数声明为裸函数-不要用
路由
装饰器装饰它们。我确实使它们成为裸函数,我也尝试重新启动应用程序,但它似乎给了我同样的错误。我怀疑你的蓝图有很多其他方法和路由是的,是的。我不确定这将如何连接到错误,但我已经用每个蓝图文件的完整代码更新了我的答案。除了
index()
之外,我还没有为
home
编写任何其他方法,因为我刚刚开始编写应用程序。
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()