Is it possible to get flask app url without request context?

233 views Asked by At

It's a URL shortener app. The app structure is like following: App structure

In forms.py, I have custom validators: validate_url() and validate_short_url() that use APP_URL; APP_URL = "localhost:5000/"

I'm fine with that running locally, but there is a lot of cases app domain can change:

  • Running through docker image;
  • Hosting (e.g. on Heroku);
  • Changing the port value;

So every time I run this flask app differently I have to change the value of APP_URL, which isn't the best practice

All in all, I want to use something like flask.Request.url_root to avoid manual writing again and again

When I just try to use flask.request I get the following traceback:

RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

forms.py is posted here

The app is already hosted on Heroku, here is the link: https://qysqa.herokuapp.com/

1

There are 1 answers

0
dosek On

The solution was to use flask.request inside custom validators (validate_url() and validate_short_url()) where app context gets passed