You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask中通过蓝图关联的profile函数未执行问题排查求助

Flask中通过蓝图关联的profile函数未执行问题排查求助

大家好,我遇到一个Flask蓝图相关的问题,折腾了好久没找到原因,想请各位帮忙排查下。

我已经把profile函数通过蓝图关联到主应用,也在create_app里完成了蓝图注册,但访问对应路由时,函数里的调试打印完全没输出,感觉这个函数根本没被触发执行。

相关代码片段

views.py

...
views_bp = Blueprint('views', __name__)

@views_bp.route('/profile')
@login_required
def profile():
    print("In profile route")
    print(f"Session: {session}")
    # debug
    if current_user.is_authenticated:
        print(f"Current user: {current_user.id}")
    else:
        print("No user authenticated.")
...

create_app.py

...
from views import views_bp
from models import User, db

login_manager = LoginManager()

def create_app():
    pymysql.install_as_MySQLdb

    app = Flask(__name__)
    @app.route('/')
    def index():
        return render_template('index.html')
    app.config['SQLALCHEMY_DATABASE_URI'] = '<db.url>'
    app.config['SECRET_KEY'] = '<my secret code>' 

    db.init_app(app)
    login_manager = LoginManager()
    login_manager.init_app(app)
    @login_manager.user_loader
    def load_user(user_id):
        return User.query.get(int(user_id))
    
    app.register_blueprint(auth_blueprint)
    app.register_blueprint(views_bp, url_prefix='/auth')

    return app
...

我尝试过的操作

  1. 在同一个views.py里新增了测试路由:
...
@views_bp.route('/test')
def test():
    return "Test Page"
...

访问http://127.0.0.1:5000/auth/test能正常显示"Test Page",说明蓝图注册本身是有效的。
2. 移除了profile路由上的@login_required装饰器,但问题依旧,调试打印还是没有任何输出。

感觉这应该是个很低级的小错误,但我自己找了好久都没发现,有没有大佬能帮我指出问题所在?如果需要补充其他代码或信息,我随时提供。

备注:内容来源于stack exchange,提问作者Егор

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:54:34