question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

URL Converters should have access to app context

See original GitHub issue

Expected Behavior

The following code should create a new URL converter that converts a specific field to a model (or None).

class ModelConverter(BaseConverter):

    def to_python(self, value):
        return models.Example.query.filter(models.Example.name == value).first()

    def to_url(self, value):
        if isinstance(value, str):
            return value
        return value.name

Actual Behavior

The converters don’t have access to the app context so an error is thrown.

Bad Solution

class OrganizationConverter(BaseConverter):
    app = None
    def to_python(self, value):
        with self.app.app_context():
            return repositories.Organization.query.filter(repositories.Organization.name == value).first()

    def to_url(self, value):
        if isinstance(value, str):
            return value
        return value.name

This solution tries to get around it by creating a new app context. The problem is that the context disappears, killing the database session at the same time. This prevents SQLAlchemy features (such as lazy loading).

Environment

  • Python version: 3.6 and 3.7
  • Flask version: 1.0.2
  • SQLAlchemy version: 1.2.17
  • Flask-SQLAlchemy version: 2.3.2
  • Werkzeug version: 0.14.1

Additional Resources

This issue has also come up on stack overflow

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
davidismcommented, Jan 31, 2019

I’ve also ran into this issue with converters, but it’s might not be straightforward to change at this point.

URL matching is done as part of creating the request context, not pushing the context:

https://github.com/pallets/flask/blob/0b5b4a66ef99c8b91569dd9b9b34911834689d3f/flask/ctx.py#L313

So we’d need to move it into RequestContext.push, after it’s actually pushed so that request is populated:

https://github.com/pallets/flask/blob/0b5b4a66ef99c8b91569dd9b9b34911834689d3f/flask/ctx.py#L379

It’s a more internal part of Flask, and I haven’t really seen any discussion about overriding or using these parts, but I’d have to understand that more before making the change.

0reactions
michaeltoohigcommented, Sep 12, 2019

Is this well documented yet? I’m here since I read the 1.1.1 release notes and was looking for some examples of how this feature could be used. The test above by @eladm26 clears things up but it took awhile for me to find it here.

Here’s the part from the release notes:

URL routing is performed after the request context is pushed. This allows custom URL converters to access current_app and request. This makes it possible to implement converters such as one that queries the database for a model based on the ID in the URL.

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Leveraging the Flask application context in a custom URL ...
This issue is given that the URL converter is outside of the app context I needed to include the following block, with app.app_context():...
Read more >
API — Flask Documentation (2.2.x)
The application will work even with __name__ , thanks to how resources are ... The URL adapter is created at a point where...
Read more >
Guide for running C# Azure Functions in an isolated worker ...
Learn how to use a .NET isolated worker process to run your C# functions in Azure, which supports non-LTS versions of .NET and...
Read more >
Django url parameters, extra options & query strings
Url parameters with Django path converters and Python regular expression named ... Url parameters can capture any part of a url, whether it's...
Read more >
Developing Python Web Applications with Flask
You can access the webapp from a browser via URL http://127.0.0.1:5000 (or http://localhost:5000 ). ... 6.5 Flask's Application Context and Request Context.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found