使用flask.g在模板之间传递值

时间:2015-07-04 17:48:15

标签: python flask

使用Flask我有一个用作等待屏幕的页面,直到用户将RFID卡放在阅读器上。将卡放在阅读器上后,user值将传递到模板中,然后用户将自动移动到另一个页面。我需要捕获值(使用我理解flask.g)并将其传递给第二页。目前我的Flask views.py看起来像这样:

@app.route('/activity', methods=['GET', 'POST'])
def activity():
    user = g.user
    location = {'mspace': 'Central Library'} 
    return render_template('activity.html',
                            location = location,
                            user = user)

@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index(): 
    global checkCheck
    print "starting"
    if request.method == 'POST':
        # print(request.data)
        checkCheck = True
        location = {'mspace': 'Central Library'} 
        user = {'nickname': request.args.get('name')}
        print "works!"
        print user['nickname']
        return render_template('index.html',
                    location = location,
                    user = user) 
    g.user = user
    return render_template('index.html', 
                            location = "test", 
                            user = 'user')

但是当卡片放置后我没有渲染第二页时,我收到错误AttributeError: '_AppCtxGlobals' object has no attribute 'user' - 为什么不是第二页识别g.user

0 个答案:

没有答案