marshmallow-code / marshmallow-code/flask-smorest

Hard to use `url_for` in a view

Open
#516 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
717
Forks
77
Avg merge
7h 49m
Merged PRs (30d)
3

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Blueprint.route registration path and Blueprint.register_views_in_doc, then trace how the registered blueprint name and view class name form the endpoint. Confirm how view classes expose registration metadata and add coverage showing that a registered view can use its full endpoint with flask.url_for without repeating the blueprint name in every view.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.