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.
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.
>
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.
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.
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!
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.
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
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.
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.
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.
>
>
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. >> > >
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.
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.