Url_for in different instances of Flask

I have a complex flask application with multiple instances of Flask submitted using werkzeug middleware. And in this situation, I have two questions related not to url_for, but to managing the context of the jars.

1) How to create a URL from one application to another?

2) The main thing is how to create a URL for a specific application without the app_context application. For example, I need to create some URL by import time or by celery order. I tried to wrap on top of all the application instances and override url_for as

def url_for(self, *args, **kwargs): with self.app.app_context(): return url_for(*args, **kwargs) 

but I just got the following error: "The application was unable to create a URL adapter for request-independent URL generation. Perhaps you can fix this by setting the SERVER_NAME configuration variable." Any suggestions?

Update: my solution for the second problem was correct, just need to add SERVER_NAME, but the first one is still open

+5
source share
1 answer

I ended up creating a separate URL builder for each application

 absolute_url_adapter = app.url_map.bind_to_environ({ 'wsgi.url_scheme': 'http', 'HTTP_HOST': app.config['SERVER_NAME'], 'SCRIPT_NAME': app.url_prefix, 'REQUEST_METHOD': 'GET', }) 

url_prefix is ​​the URL at which the dispatcher sends requests

Then in each application you use it that way

 absolute_url_adapter.build('main.main', force_external=True) 
+1
source

Source: https://habr.com/ru/post/1215946/


All Articles