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

Closed
Artucuno opened this issue Jun 28, 2022 · 3 comments
Closed

Blueprint Support #64

Artucuno opened this issue Jun 28, 2022 · 3 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.

Copy link

dosubot bot commented Nov 26, 2024

Hi, @Artucuno. I'm Dosu, and I'm helping the Flask-Discord team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You reported an AttributeError when using Flask-Discord within a Flask blueprint.
  • You suspected the issue was due to the use of current_app instead of the blueprint context.
  • User xybl3 suggested moving DiscordOAuth2Session initialization to the main file to avoid circular imports.
  • You resolved the issue independently by using oauthlib to implement the functionality from scratch.

Next Steps:

  • Is this issue still relevant to the latest version of the Flask-Discord repository? If so, please comment to keep the discussion open.
  • Otherwise, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Nov 26, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Dec 3, 2024
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