librelist archives

« back to archive

How coule I use 'g' object?

How coule I use 'g' object?

From:
Robin
Date:
2013-08-11 @ 13:53
when i make a login function like this:
( PS: you can read code here: http://chopapp.com/#xr7p13tq )

@account.route('/signup', methods=['GET', 'POST'])
def signup():
    if request.method == 'POST':
        mail = request.form['mail']
        pwd = request.form['pwd']

        ex_user = User.query.filter_by(mail=mail).first()
        if ex_user:
            if pwd == ex_user.pwd:
                session['user_id'] = ex_user.id
                g.user = ex_user
                flash('Success! ')
                return redirect(url_for('account.me'))
            else:
                flash('Wrong passwords!')
        else:
            flash('User not exist!')

    return render_template('account/signup.html')

@account.route('/me', methods=['GET', 'POST'])
def me():
    print session['user_id']
    print g.user  #AttributeError: '_RequestGlobals' object has no
attribute 'user'
    return render_template('account/me.html', user = g.user)


I can't get g.user in 'me' section,  why?

Thanks :)
-- 
http://about.me/rlog/bio

回复: [flask] How coule I use 'g' object?

From:
linnchord
Date:
2013-08-11 @ 15:19
sample code

def login_required(f):
    @wraps(f)
    def do(*args, **kwargs):
        g.user_id = session.get('user_id')
        if g.user_id:
            g.user = User.get(g.user_id)
        else:
            abort(401)
        return f(*args, **kwargs)
    return do

@account.route('/me', methods=['GET', 'POST'])
@login_required
def me():
    print session['user_id']
    print g.user  #AttributeError: '_RequestGlobals' object has no 
attribute 'user'
    return render_template('account/me.html', user = g.user)  


===

linnchord@gmail.com



在 2013年8月11日星期日,下午9:53,Robin 写道:

> when i make a login function like this:
> ( PS: you can read code here: http://chopapp.com/#xr7p13tq )
>  
> @account.route('/signup', methods=['GET', 'POST'])
> def signup():
>     if request.method == 'POST':
>         mail = request.form['mail']
>         pwd = request.form['pwd']
>  
>         ex_user = User.query.filter_by(mail=mail).first()
>         if ex_user:
>             if pwd == ex_user.pwd:
>                 session['user_id'] = ex_user.id (http://ex_user.id)
>                 g.user = ex_user
>                 flash('Success! ')
>                 return redirect(url_for('account.me (http://account.me)'))
>             else:
>                 flash('Wrong passwords!')
>         else:
>             flash('User not exist!')
>  
>     return render_template('account/signup.html')
>  
> @account.route('/me', methods=['GET', 'POST'])
> def me():
>     print session['user_id']
>     print g.user  #AttributeError: '_RequestGlobals' object has no 
attribute 'user'
>     return render_template('account/me.html', user = g.user)
>  
>  
> I can't get g.user in 'me' section,  why?
>  
> Thanks :)
> --  
> http://about.me/rlog/bio

回复: [flask] How coule I use 'g' object?

From:
Robin Deng
Date:
2013-08-12 @ 07:25
thanks, All!  i got it  :)

--  
Robin Deng

已使用 Sparrow (http://www.sparrowmailapp.com/?sig)  

在 2013年8月11日星期日,下午11:19,linnchord 写道:

> sample code
>  
> def login_required(f):
>     @wraps(f)
>     def do(*args, **kwargs):
>         g.user_id = session.get('user_id')
>         if g.user_id:
>             g.user = User.get(g.user_id)
>         else:
>             abort(401)
>         return f(*args, **kwargs)
>     return do
>  
> @account.route('/me', methods=['GET', 'POST'])
> @login_required
> def me():
>     print session['user_id']
>     print g.user  #AttributeError: '_RequestGlobals' object h as no 
attribute 'user'
>     return render_template('account/me.html', user = g.user)  
>  
>  
> ===
>  
> linnchord@gmail.com (mailto:linnchord@gmail.com)
>  
>  
>  
> 在 2013年8月11日星期日,下午9:53,Robin 写道:
>  
> > when i make a login function like this:
> > ( PS: you can read code here: http://chopapp.com/#xr7p13tq )
> >  
> > @account.route('/signup', methods=['GET', 'POST'])
> > def signup():
> >     if request.method == 'POST':
> >         mail = request.form['mail']
> >         pwd = request.form['pwd']
> >  
> >         ex_user = User.query.filter_by(mail=mail).first()
> >         if ex_user:
> >             if pwd == ex_user.pwd:
> >                 session['user_id'] = ex_user.id (http://ex_user.id)
> >                 g.user = ex_user
> >                 flash('Success! ')
> >                 return redirect(url_for('account.me (http://account.me)'))
> >             else:
> >                 flash('Wrong passwords!')
> >         else:
> >             flash('User not exist!')
> >  
> >     return render_template('account/signup.html')
> >  
> > @account.route('/me', methods=['GET', 'POST'])
> > def me():
> >     print session['user_id']
> >     print g.user  #AttributeError: '_RequestGlobals' object has no 
attribute 'user'
> >     return render_template('account/me.html', user = g.user)
> >  
> >  
> > I can't get g.user in 'me' section,  why?
> >  
> > Thanks :)
> > --  
> > http://about.me/rlog/bio
>  

Re: [flask] How coule I use 'g' object?

From:
Andy Dirnberger
Date:
2013-08-11 @ 14:06
`g` is part of the application context which is cleared after each 
request. Returning the redirect ends the current one. Once the browser 
receives the redirect, it will initiate a new request. 


You will need to assign `g.user` as part of each request.

On Sun, Aug 11, 2013 at 9:56 AM, Robin <gl.deng@gmail.com> wrote:

> when i make a login function like this:
> ( PS: you can read code here: http://chopapp.com/#xr7p13tq )
> @account.route('/signup', methods=['GET', 'POST'])
> def signup():
>     if request.method == 'POST':
>         mail = request.form['mail']
>         pwd = request.form['pwd']
>         ex_user = User.query.filter_by(mail=mail).first()
>         if ex_user:
>             if pwd == ex_user.pwd:
>                 session['user_id'] = ex_user.id
>                 g.user = ex_user
>                 flash('Success! ')
>                 return redirect(url_for('account.me'))
>             else:
>                 flash('Wrong passwords!')
>         else:
>             flash('User not exist!')
>     return render_template('account/signup.html')
> @account.route('/me', methods=['GET', 'POST'])
> def me():
>     print session['user_id']
>     print g.user  #AttributeError: '_RequestGlobals' object has no
> attribute 'user'
>     return render_template('account/me.html', user = g.user)
> I can't get g.user in 'me' section,  why?
> Thanks :)
> -- 
> http://about.me/rlog/bio

Re: [flask] How coule I use 'g' object?

From:
Ryan O'Donnell
Date:
2013-08-11 @ 15:12
To expand on Andy, you can do this with the before and after request
decorators.


On Sun, Aug 11, 2013 at 10:06 AM, Andy Dirnberger <dirn@dirnonline.com>wrote:

> `g` is part of the application context which is cleared after each
> request. Returning the redirect ends the current one. Once the browser
> receives the redirect, it will initiate a new request.
>
> You will need to assign `g.user` as part of each request.
>
>
> On Sun, Aug 11, 2013 at 9:56 AM, Robin <gl.deng@gmail.com> wrote:
>
>> when i make a login function like this:
>> ( PS: you can read code here: http://chopapp.com/#xr7p13tq )
>>
>> @account.route('/signup', methods=['GET', 'POST'])
>> def signup():
>>     if request.method == 'POST':
>>         mail = request.form['mail']
>>         pwd = request.form['pwd']
>>
>>         ex_user = User.query.filter_by(mail=mail).first()
>>         if ex_user:
>>             if pwd == ex_user.pwd:
>>                 session['user_id'] = ex_user.id
>>                 g.user = ex_user
>>                 flash('Success! ')
>>                 return redirect(url_for('account.me'))
>>             else:
>>                 flash('Wrong passwords!')
>>         else:
>>             flash('User not exist!')
>>
>>     return render_template('account/signup.html')
>>
>> @account.route('/me', methods=['GET', 'POST'])
>> def me():
>>     print session['user_id']
>>     print g.user  #AttributeError: '_RequestGlobals' object has no
>> attribute 'user'
>>     return render_template('account/me.html', user = g.user)
>>
>>
>> I can't get g.user in 'me' section,  why?
>>
>> Thanks :)
>> --
>> http://about.me/rlog/bio
>>
>
>