在Flask中通过会话将两个模块路由到相同的URL

1 投票
1 回答
1702 浏览
提问于 2025-04-17 15:23

我有一个未登录的模块/蓝图,叫做 welcome,还有一个已登录的蓝图,叫做 home。我希望一个有有效会话的用户能访问 home.index,而一个游客用户能访问 welcome.index。但是,我遇到了问题,因为这两个功能都指向同一个网址 /

我该如何实现这个功能呢?我尝试在 welcome 蓝图的 index() 函数中添加:

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

但这当然会导致循环重定向,因为 home.index 的网址和 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"

1 个回答

3

把你的方法拆分开,先检查用户是否登录,然后再调用相应的函数(每个函数需要的参数也要加上):

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

撰写回答