librelist archives

« back to archive

Can't generate URLs testing Flask from the command line?

Can't generate URLs testing Flask from the command line?

From:
Adam Hogan
Date:
2012-12-23 @ 05:52
Hi guys,

I'm trying to test a function in the console before including it in my 
flask app.  So I've run python, and imported the views, models, and 
function files.

However, when I try to run a function that calls url_for("profile", 
username=username), I get the following error:

"RuntimeError: Attempted to generate a URL with the application context 
being pushed.  This has to be executed "

Is it not possible to generate URLs, umm, out of context like this? If 
so, how would I accomplish this?  A google of this error message only 
returned links to this issue, #623, in the bug tracker:
https://github.com/mitsuhiko/flask/issues/623

Thanks!
Adam

Re: [flask] Can't generate URLs testing Flask from the command line?

From:
Smartboy
Date:
2012-12-23 @ 07:21
Try reading through here: http://flask.pocoo.org/docs/testing/

I use unit tests to test all the interfaces in the REST interface for my
flask project. What's important, though, is that you use app.test_client()
to get a client you can submit queries to interactively or via tests.
Probably what's happening is that your app isn't initialized like you think
it should be.

Smartboy

On Sat, Dec 22, 2012 at 9:52 PM, Adam Hogan <hoganaj.junk@gmail.com> wrote:

>  Hi guys,
>
> I'm trying to test a function in the console before including it in my
> flask app.  So I've run python, and imported the views, models, and
> function files.
>
> However, when I try to run a function that calls url_for("profile",
> username=username), I get the following error:
>
> "RuntimeError: Attempted to generate a URL with the application context
> being pushed.  This has to be executed "
>
> Is it not possible to generate URLs, umm, out of context like this?  If
> so, how would I accomplish this?  A google of this error message only
> returned links to this issue, #623, in the bug tracker:
> https://github.com/mitsuhiko/flask/issues/623
>
> Thanks!
> Adam
>

Re: [flask] Can't generate URLs testing Flask from the command line?

From:
Adam Hogan
Date:
2013-04-21 @ 17:28
I still don't quite understand how request contexts work after reading both
http://flask.pocoo.org/docs/testing/
http://flask.pocoo.org/docs/shell/
Could I ask for a bit of clarification on how to call the functions 
decorated in my views?

I'm trying to debug why my application, remotely hosted, doesn't work, 
and don't get any more information than a 500 error.  So, I type...

$ ./shell.py
 >>> ctx = app.test_request_context()
 >>> ctx.push()

 >>> ctx.app.test_client().get('/')
<Response streamed [500 INTERNAL SERVER ERROR]>

 >>> myappsname.app.test_client().get('/')
<Response streamed [500 INTERNAL SERVER ERROR]>

(What's the difference between calling my app by name, and using ctx 
context?  The idea is a test request context keeps your session, g 
object accessible and together, as if fired by the same browser?  What 
is the object myappsname in a shell?  Is it also a test_request_context 
somehow?)

Also, I see the ctx is working in the sense that the url maps, etc...

 >>> ctx.app.url_map
Map([<Rule '/forgotusername' (HEAD, POST, OPTIONS, GET) -> forgotusername>,
  <Rule '/forgotpassword' (HEAD, POST, OPTIONS, GET) -> forgotpassword>,

But I get a fail calling for the index page that doesn't happen when I 
run locally.

Thanks!
Adam

On 12/23/2012 02:21 AM, Smartboy wrote:
> Try reading through here: http://flask.pocoo.org/docs/testing/
>
> I use unit tests to test all the interfaces in the REST interface for 
> my flask project. What's important, though, is that you use 
> app.test_client() to get a client you can submit queries to 
> interactively or via tests. Probably what's happening is that your app 
> isn't initialized like you think it should be.
>
> Smartboy
>
> On Sat, Dec 22, 2012 at 9:52 PM, Adam Hogan <hoganaj.junk@gmail.com 
> <mailto:hoganaj.junk@gmail.com>> wrote:
>
>     Hi guys,
>
>     I'm trying to test a function in the console before including it
>     in my flask app.  So I've run python, and imported the views,
>     models, and function files.
>
>     However, when I try to run a function that calls
>     url_for("profile", username=username), I get the following error:
>
>     "RuntimeError: Attempted to generate a URL with the application
>     context being pushed.  This has to be executed "
>
>     Is it not possible to generate URLs, umm, out of context like
>     this?  If so, how would I accomplish this?  A google of this error
>     message only returned links to this issue, #623, in the bug tracker:
>     https://github.com/mitsuhiko/flask/issues/623
>
>     Thanks!
>     Adam
>
>

Re: [flask] Can't generate URLs testing Flask from the command line?

From:
Simon Sapin
Date:
2012-12-23 @ 10:17
Le 23/12/2012 06:52, Adam Hogan a écrit :
> However, when I try to run a function that calls url_for("profile",
> username=username), I get the following error:
>
> "RuntimeError: Attempted to generate a URL with the application context
> being pushed.  This has to be executed "

Hi,

As the message says, you need a context to use url_for(), so create one. 
It’s explained here:

http://flask.pocoo.org/docs/shell/

A (test) request context also creates an app context so that would work. 
But if you don’t need a request, you can create just an app context with 
ctx = app.app_context(). (You still need to .push() it or use a 'with' 
statement.)

Cheers,
-- 
Simon Sapin