Flask Application was not able to create a URL adapter for request [duplicate]

2024/10/9 18:22:00

I have this code which used to work:

import unittestfrom flask import url_forfrom dm.web import create_app, dbclass TestApi(unittest.TestCase):def setUp(self):"""Create and configure a new app instance for each test."""self.app = create_app('test')self.app_context = self.app.app_context()self.app_context.push()db.create_all()self.client = self.app.test_client(use_cookies=True)def tearDown(self) -> None:db.session.remove()db.drop_all()self.app_context.pop()def test_url(self):self.assertEqual('/', url_for('root.home'))if __name__ == '__main__':unittest.main()

But since today I am getting this error:

ERROR: test_url (__main__.TestApi)
----------------------------------------------------------------------
Traceback (most recent call last):File "/home/joan/.PyCharmCE2019.3/config/scratches/scratch.py", line 17, in test_urlself.assertEqual('/', url_for('root.home'))File "/home/joan/venvs/dimensigon3.7/lib/python3.7/site-packages/flask/helpers.py", line 333, in url_for"Application was not able to create a URL adapter for request"
RuntimeError: Application was not able to create a URL adapter for request independent URL generation. You might be able to fix this by setting the SERVER_NAME config variable.

From what I see in the url_for, I am getting None on the appctx.url_adapter which I don't know when it is set.

Thanks!

Answer

As per this blog post, you could try this pattern, which gives you both an app context and a temporary request context:

    def test_url(self):with app.app_context(), app.test_request_context():self.assertEqual('/', url_for('root.home'))
https://en.xdnf.cn/q/69987.html

Related Q&A

Python typing deprecation

The latest typing docs has a lot of deprecation notices like the following: class typing.Deque(deque, MutableSequence[T]) A generic version of collections.deque.New in version 3.5.4.New in version 3.6.…

Cant install tensorflow with pip or anaconda

Does anyone know how to properly install tensorflow on Windows?Im currently using Python 3.7 (also tried with 3.6) and every time I get the same "Could not find a version that satisfies the requi…

send xml file to http using python

how can i send an xml file on my system to an http server using python standard library??

Why python Wnck window.activate(int(time.time()))

This to me is VERY strange. Could someone please explain why the activate() function should want a timestamp? Wouldnt 99.9% of the time be NOW or ASAP or "At your earliest convenience"? And…

Regex subsequence matching

Im using python but code in any language will do as well for this question.Suppose I have 2 strings. sequence =abcd string = axyzbdclkdIn the above example sequence is a subsequence of stringHow can I …

Read a Bytes image from Amazon Kinesis output in python

I used imageio.get_reader(BytesIO(a), ffmpeg) to load a bytes image and save it as normal image.But the below error throws when I read the image using imageio.get_reader(BytesIO(a), ffmpeg)Traceback …

How to compute optical flow using tvl1 opencv function

Im trying to find python example for computing optical flow with tvl1 opencv function createOptFlow_DualTVL1 but it seems that there isnt enough documentation for it.Could anyone please let me do that?…

Python 3.3.4: python-daemon-3K ; How to use runner

Struggling to try and get a python daemon to work using Python 3.3.4. Im using the latest version of the python-daemon-3K from PyPi i.e. 1.5.8Starting point is the following code found How do you creat…

How to select specific data variables from xarray dataset

BACKGROUND I am trying to download GFS weather data netcdf4 files via xarray & OPeNDAP. Big thanks to Vorticity0123 for their prior post, which allowed me to get the bones of the python script sort…

List object has no attribute Values error

I would like to get the data to Excel worksheet. The problem is when I run the whole code I receive an error but when I run it separately no error it works. Here is what I want; from xlwings import Wor…