【问题标题】:how to implement Flask-Dance with Flask Blueprints如何使用 Flask 蓝图实现 Flask-Dance
【发布时间】:2021-05-23 10:02:21
【问题描述】:

我尝试将 Flask-Dance 与普通的烧瓶应用程序一起使用,它可以工作,如果我尝试使用烧瓶蓝图来实现,它就不起作用。如何将烧瓶舞注册到烧瓶蓝图?

身份验证蓝图的我的views.py

from flask import render_template, url_for, redirect, current_app, request
from app.auth import auth
from flask_dance.contrib import github

@auth.route('/login')
def login():
return render_template('auth/login.html')

@auth.route("/")
def github():
    if not github.authorized:
        return redirect(url_for("github.login"))
    resp = github.get("/user")
    assert resp.ok
    return "You are @{login} on GitHub".format(login=resp.json()["login"])

我的 init.py 用于身份验证蓝图

from flask import Blueprint
from flask_dance.contrib.github import make_github_blueprint, github

auth = Blueprint('auth', __name__, url_prefix='/auth')

blueprint = make_github_blueprint(client_id="m-client-id",client_secret="my-client-secret")

auth.register_blueprint(blueprint, url_prefix="/auth")



from app.auth import views

和我的主 init.py 文件:

from flask import Flask
from flask_fontawesome import FontAwesome

from app.config import Config

fa = FontAwesome()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)
    fa.init_app(app)
    
    from app.public import public
    app.register_blueprint(public)
    
    from app.auth import auth
    app.register_blueprint(auth)
    
    return app

【问题讨论】:

    标签: python flask flask-dance


    【解决方案1】:

    首先你应该为 github 创建和注册不同的蓝图。

    github/init.py

    from flask_dance.contrib import github
    from flask_dance.contrib.github import make_github_blueprint
    
    github_blueprint = make_github_blueprint(client_id='your-client-id',client_secret='your-client-secret')
    
    
    
    from app.github import views
    

    github/views.py

    @github_blueprint.route("/")
    def github_login():
         if not github.authorized:
              return redirect(url_for('github.login'))
         account_info = github.get('/user')
         if account_info.ok:
              account = account_info.json()
              return '<h1>Your Github name is {}'.format(account['login'])
    

    最后在您的主 init.py 文件中

    from flask import Flask
    from flask_fontawesome import FontAwesome
    
    from app.config import Config
    
    fa = FontAwesome()
    
    def create_app(config_class=Config):
          app = Flask(__name__)
          app.config.from_object(Config)
          fa.init_app(app)
    
          from app.public import public
          app.register_blueprint(public)
    
          from app.auth import auth
          app.register_blueprint(auth)
    
          from app.github import github_blueprint
          app.register_blueprint(github_blueprint,  url_prefix='/github_login')
          #/github_login=callback url
    
          return app
    

    【讨论】:

    • 你将如何访问 github/view.py 中的@github_blueprint?该变量在不同的文件中声明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多