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

Hard to use url_for in a view #516

Open
juledwar opened this issue May 30, 2023 · 2 comments
Open

Hard to use url_for in a view #516

juledwar opened this issue May 30, 2023 · 2 comments
Labels

Comments

@juledwar
Copy link

I'm trying to convert my Flask app from flask-restful, and I need to use flask.url_for in some of my pagination helper code that's in my views' base class.

The endpoint value that url_for takes is basically {blueprint.name}.{viewclass.__name__} and in fact this value is calculated and used in the Blueprint.register_views_in_doc method. However while it''s easy to get the view class name, the blueprint name under which the view is registered is not available to the view itself, which means the endpoint name can't be calculated.

At the moment I am basically copy/pasting this same code in every view definition:

    @classmethod
    @property
    def full_endpoint(cls):
           return f'{bp.name}.{cls.__name__}'

but I'd ideally like to make this more automatic by injecting the full_endpoint property when the Blueprint.route code runs to register the view.

@lafrech
Copy link
Member

lafrech commented Jun 4, 2023

Can't you set this property in the base class?

I guess a more comprehensive code sample would help explaining the problem.

@juledwar
Copy link
Author

juledwar commented Jun 4, 2023

Can't you set this property in the base class?

You can't do that because the blueprint is not known/available there.

I guess a more comprehensive code sample would help explaining the problem.

The bottom line is that the view class doesn't know to which blueprint it's attached, so views cannot calculate their full endpoint, I need this copypasta code in every view. Instead, it could be refactored in a base view class in such a way that it's calculated when needed.

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

No branches or pull requests

2 participants