Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blueprint Support #64

Open
Artucuno opened this issue Jun 28, 2022 · 2 comments
Open

Blueprint Support #64

Artucuno opened this issue Jun 28, 2022 · 2 comments

Comments

@Artucuno
Copy link

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

@xybl3
Copy link

xybl3 commented Aug 19, 2022

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)

@Artucuno
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants