librelist archives

« back to archive

name 'response' not defined

name 'response' not defined

From:
Michal Czerwinski
Date:
2010-11-24 @ 17:47
Hi Folks.
A quick one.

As an example I am just following the http://flask.pocoo.org/snippets/11/
where you can find:

@app.after_request(response)
def close_connection():
    g.db.close()

    return response

You can also find this piece of code that operates on response object in
relation to other examples and features etc., but when I am trying to use
this, I just get

    @app.after_request(response)
NameError: name 'response' is not defined

Any ideas?

-- 
Mc.

Re: [flask] name 'response' not defined

From:
Anton Khodakivskiy
Date:
2010-11-24 @ 17:58
Not 100% sure but I think that the correct syntax is as follows:

@app.after_request()
def close_connection(response):
    ... your code ...
    return response

On Wed, Nov 24, 2010 at 12:47 PM, Michal Czerwinski <holmier@gmail.com>wrote:

> Hi Folks.
> A quick one.
>
> As an example I am just following the http://flask.pocoo.org/snippets/11/
> where you can find:
>
> @app.after_request(response)
>
> def close_connection():
>     g.db.close()
>
>
>     return response
>
> You can also find this piece of code that operates on response object in
> relation to other examples and features etc., but when I am trying to use
> this, I just get
>
>     @app.after_request(response)
> NameError: name 'response' is not defined
>
> Any ideas?
>
> --
> Mc.
>

Re: [flask] name 'response' not defined

From:
Rodrigo Aliste P.
Date:
2010-11-24 @ 18:16
That's the correct syntax, the snippet is wrong.

2010/11/24 Anton Khodakivskiy <akhodakivskiy@gmail.com>

> Not 100% sure but I think that the correct syntax is as follows:
>
> @app.after_request()
> def close_connection(response):
>     ... your code ...
>     return response
>
>
> On Wed, Nov 24, 2010 at 12:47 PM, Michal Czerwinski <holmier@gmail.com>wrote:
>
>> Hi Folks.
>> A quick one.
>>
>> As an example I am just following the http://flask.pocoo.org/snippets/11/
>> where you can find:
>>
>> @app.after_request(response)
>>
>> def close_connection():
>>     g.db.close()
>>
>>
>>     return response
>>
>> You can also find this piece of code that operates on response object in
>> relation to other examples and features etc., but when I am trying to use
>> this, I just get
>>
>>     @app.after_request(response)
>> NameError: name 'response' is not defined
>>
>> Any ideas?
>>
>> --
>> Mc.
>>
>
>


-- 
Rodrigo Aliste P.

Re: [flask] name 'response' not defined

From:
Michal Czerwinski
Date:
2010-11-24 @ 18:35
 I get:
TypeError: after_request() takes exactly 2 arguments (1 given)

I checked API doc it says:

after_request(f)
Register a function to be run after each request.

On Wed, Nov 24, 2010 at 6:16 PM, Rodrigo Aliste P. <raliste@gmail.com>wrote:

> That's the correct syntax, the snippet is wrong.
>
> 2010/11/24 Anton Khodakivskiy <akhodakivskiy@gmail.com>
>
> Not 100% sure but I think that the correct syntax is as follows:
>>
>> @app.after_request()
>> def close_connection(response):
>>     ... your code ...
>>     return response
>>
>>
>> On Wed, Nov 24, 2010 at 12:47 PM, Michal Czerwinski <holmier@gmail.com>wrote:
>>
>>> Hi Folks.
>>> A quick one.
>>>
>>> As an example I am just following the
>>> http://flask.pocoo.org/snippets/11/
>>> where you can find:
>>>
>>> @app.after_request(response)
>>>
>>> def close_connection():
>>>     g.db.close()
>>>
>>>
>>>     return response
>>>
>>> You can also find this piece of code that operates on response object in
>>> relation to other examples and features etc., but when I am trying to use
>>> this, I just get
>>>
>>>     @app.after_request(response)
>>> NameError: name 'response' is not defined
>>>
>>> Any ideas?
>>>
>>> --
>>> Mc.
>>>
>>
>>
>
>
> --
> Rodrigo Aliste P.
>
>


-- 
Mc.

Re: [flask] name 'response' not defined

From:
Dag Odenhall
Date:
2010-11-24 @ 21:03
On Wed, 2010-11-24 at 18:35 +0000, Michal Czerwinski wrote:

>  I get:
> TypeError: after_request() takes exactly 2 arguments (1 given)
> 
> I checked API doc it says:
> 
> after_request(f)
> Register a function to be run after each request. 


That'd be the self (the instance) and the callback:
Flask.after_request(app, func). Instance methods are usually called as
instance.method() though so make that app.after_request(func); that on
the other hand is used as a decorator:


@app.after_request
def callback...


Now, the callback itself can take arguments as well, in this case the
response returned by  the view function:


@app.after_request
def do_some_processing(response):
    # Do stuff, possibly modify "response"
    return response  # or the request will fail


Happy Flasking!

Re: [flask] name 'response' not defined

From:
Michal Czerwinski
Date:
2010-11-24 @ 21:20
On Wed, Nov 24, 2010 at 9:03 PM, Dag Odenhall <dag.odenhall@gmail.com>wrote:

> (...) That'd be the self (the instance) and the callback: (...)
>
> Happy Flasking!
>

Sweet. Thanks a lot for a brief summary!
-- 
Mc.

Re: [flask] name 'response' not defined

From:
Armin Ronacher
Date:
2010-11-24 @ 18:18
Hi,

On 11/24/10 7:16 PM, Rodrigo Aliste P. wrote:
> That's the correct syntax, the snippet is wrong.
I took the liberty to fix the snippet.


Regards,
Armin

Re: [flask] name 'response' not defined

From:
Michal Czerwinski
Date:
2010-11-24 @ 18:36
I think it is also worth fixing the Config module usage as it does not bring
any value there.
Just enclosing in quotes should do the thing.


On Wed, Nov 24, 2010 at 6:18 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 11/24/10 7:16 PM, Rodrigo Aliste P. wrote:
> > That's the correct syntax, the snippet is wrong.
> I took the liberty to fix the snippet.
>
>
> Regards,
> Armin
>



-- 
Mc.

Re: [flask] name 'response' not defined

From:
Rodrigo Aliste P.
Date:
2010-11-24 @ 18:39
I'm sorry, the correct syntax, as it's a decorator, is:

@app.before_request
def your_view():
    return 'Something'

2010/11/24 Michal Czerwinski <holmier@gmail.com>

> I think it is also worth fixing the Config module usage as it does not
> bring any value there.
> Just enclosing in quotes should do the thing.
>
>
> On Wed, Nov 24, 2010 at 6:18 PM, Armin Ronacher <
> armin.ronacher@active-4.com> wrote:
>
>> Hi,
>>
>> On 11/24/10 7:16 PM, Rodrigo Aliste P. wrote:
>> > That's the correct syntax, the snippet is wrong.
>> I took the liberty to fix the snippet.
>>
>>
>> Regards,
>> Armin
>>
>
>
>
> --
> Mc.
>



-- 
Rodrigo Aliste P.

Re: [flask] name 'response' not defined

From:
Anton Khodakivskiy
Date:
2010-11-24 @ 18:51
And to make sure that the question is answered properly, for the
after_request decorator:

@app.after_request
def close_connection(response):
    g.db.close()
    return response

Note the absence of the brackets after the decorator!

P.S.
This thread once again proves that a group of people working on one thing
utilize an intersection of their skills, not a union xD.

On Wed, Nov 24, 2010 at 1:39 PM, Rodrigo Aliste P. <raliste@gmail.com>wrote:

> I'm sorry, the correct syntax, as it's a decorator, is:
>
> @app.before_request
> def your_view():
>     return 'Something'
>
> 2010/11/24 Michal Czerwinski <holmier@gmail.com>
>
> I think it is also worth fixing the Config module usage as it does not
>> bring any value there.
>> Just enclosing in quotes should do the thing.
>>
>>
>> On Wed, Nov 24, 2010 at 6:18 PM, Armin Ronacher <
>> armin.ronacher@active-4.com> wrote:
>>
>>> Hi,
>>>
>>> On 11/24/10 7:16 PM, Rodrigo Aliste P. wrote:
>>> > That's the correct syntax, the snippet is wrong.
>>> I took the liberty to fix the snippet.
>>>
>>>
>>> Regards,
>>> Armin
>>>
>>
>>
>>
>> --
>> Mc.
>>
>
>
>
> --
> Rodrigo Aliste P.
>
>

Re: [flask] name 'response' not defined

From:
danjac354@gmail.com
Date:
2010-11-24 @ 19:07
Wow, forgot I wrote that snippet. Thanks for fixing it :-)

On 24 November 2010 18:51, Anton Khodakivskiy <akhodakivskiy@gmail.com> wrote:
> And to make sure that the question is answered properly, for the
> after_request decorator:
>
> @app.after_request
> def close_connection(response):
>     g.db.close()
>     return response
>
> Note the absence of the brackets after the decorator!
>
> P.S.
> This thread once again proves that a group of people working on one thing
> utilize an intersection of their skills, not a union xD.
>
> On Wed, Nov 24, 2010 at 1:39 PM, Rodrigo Aliste P. <raliste@gmail.com>
> wrote:
>>
>> I'm sorry, the correct syntax, as it's a decorator, is:
>> @app.before_request
>> def your_view():
>>     return 'Something'
>>
>> 2010/11/24 Michal Czerwinski <holmier@gmail.com>
>>>
>>> I think it is also worth fixing the Config module usage as it does not
>>> bring any value there.
>>> Just enclosing in quotes should do the thing.
>>>
>>>
>>> On Wed, Nov 24, 2010 at 6:18 PM, Armin Ronacher
>>> <armin.ronacher@active-4.com> wrote:
>>>>
>>>> Hi,
>>>>
>>>> On 11/24/10 7:16 PM, Rodrigo Aliste P. wrote:
>>>> > That's the correct syntax, the snippet is wrong.
>>>> I took the liberty to fix the snippet.
>>>>
>>>>
>>>> Regards,
>>>> Armin
>>>
>>>
>>>
>>> --
>>> Mc.
>>
>>
>>
>> --
>> Rodrigo Aliste P.
>>
>
>

Re: [flask] name 'response' not defined

From:
Rodrigo Aliste P.
Date:
2010-11-24 @ 19:34
Hehe sorry, you are right. The important practical thing is not having
backet after the decorator!



2010/11/24 danjac354@gmail.com <danjac354@gmail.com>

> Wow, forgot I wrote that snippet. Thanks for fixing it :-)
>
> On 24 November 2010 18:51, Anton Khodakivskiy <akhodakivskiy@gmail.com>
> wrote:
> > And to make sure that the question is answered properly, for the
> > after_request decorator:
> >
> > @app.after_request
> > def close_connection(response):
> >     g.db.close()
> >     return response
> >
> > Note the absence of the brackets after the decorator!
> >
> > P.S.
> > This thread once again proves that a group of people working on one thing
> > utilize an intersection of their skills, not a union xD.
> >
> > On Wed, Nov 24, 2010 at 1:39 PM, Rodrigo Aliste P. <raliste@gmail.com>
> > wrote:
> >>
> >> I'm sorry, the correct syntax, as it's a decorator, is:
> >> @app.before_request
> >> def your_view():
> >>     return 'Something'
> >>
> >> 2010/11/24 Michal Czerwinski <holmier@gmail.com>
> >>>
> >>> I think it is also worth fixing the Config module usage as it does not
> >>> bring any value there.
> >>> Just enclosing in quotes should do the thing.
> >>>
> >>>
> >>> On Wed, Nov 24, 2010 at 6:18 PM, Armin Ronacher
> >>> <armin.ronacher@active-4.com> wrote:
> >>>>
> >>>> Hi,
> >>>>
> >>>> On 11/24/10 7:16 PM, Rodrigo Aliste P. wrote:
> >>>> > That's the correct syntax, the snippet is wrong.
> >>>> I took the liberty to fix the snippet.
> >>>>
> >>>>
> >>>> Regards,
> >>>> Armin
> >>>
> >>>
> >>>
> >>> --
> >>> Mc.
> >>
> >>
> >>
> >> --
> >> Rodrigo Aliste P.
> >>
> >
> >
>



-- 
Rodrigo Aliste P.

Re: [flask] name 'response' not defined

From:
Michal Czerwinski
Date:
2010-11-24 @ 18:48
It is an app.after_request we are talking here about :)

On Wed, Nov 24, 2010 at 6:39 PM, Rodrigo Aliste P. <raliste@gmail.com>wrote:

> I'm sorry, the correct syntax, as it's a decorator, is:
>
> @app.before_request
> def your_view():
>     return 'Something'
>
> 2010/11/24 Michal Czerwinski <holmier@gmail.com>
>
> I think it is also worth fixing the Config module usage as it does not
>> bring any value there.
>> Just enclosing in quotes should do the thing.
>>
>>
>> On Wed, Nov 24, 2010 at 6:18 PM, Armin Ronacher <
>> armin.ronacher@active-4.com> wrote:
>>
>>> Hi,
>>>
>>> On 11/24/10 7:16 PM, Rodrigo Aliste P. wrote:
>>> > That's the correct syntax, the snippet is wrong.
>>> I took the liberty to fix the snippet.
>>>
>>>
>>> Regards,
>>> Armin
>>>
>>
>>
>>
>> --
>> Mc.
>>
>
>
>
> --
> Rodrigo Aliste P.
>
>


-- 
Mc.