Flask-Discord icon indicating copy to clipboard operation
Flask-Discord copied to clipboard

Blueprint Support

Open Artucuno opened this issue 2 years ago • 2 comments

Hey, I need to use Flask-Discord in a blueprint but I am not sure if that is possible.

I get the error AttributeError: 'Flask' object has no attribute 'discord' and I think its because it uses current_app instead of the blueprint itself.

Thanks

Artucuno avatar Jun 28 '22 09:06 Artucuno

Hello, I had the same problem with quart_discord. All you need to do is move

discord = DiscordOAuth2Session(app)

to your main file, and then import discord from main in your blueprint file. Also to prevent circular imports remember to wrap import of blueprint and app.run() in if statement, something like that

if __name__ == "__main__":
    from blueprints.api import api
    from blueprints.dashboard import dashboard

    app.register_blueprint(api)
    app.register_blueprint(dashboard)


    app.run(host="localhost", port=5000, debug=True)

For me it works perfectly

Your blueprint should look like that

...
from flask.blueprints import Blueprint
from main import app, discord

dashboard = Blueprint('dashboard', __name__, url_prefix='/dashboard', template_folder="../templates/dashboard/")
...

And your main

app = Flask(__name__)

...

discord = DiscordOAuth2Session(app)

if __name__ == "__main__":
    from blueprints.api import api
    from blueprints.dashboard import dashboard

    app.register_blueprint(api)
    app.register_blueprint(dashboard)


    app.run(host="localhost", port=5000, debug=True)

xybl3 avatar Aug 19 '22 20:08 xybl3

Hey, thanks for your reply. I fixed the issue in the end by just using oauthlib and doing it from scratch just because for my case, I can't have discord imported in the main file.

Artucuno avatar Aug 21 '22 05:08 Artucuno