`

Flask, set a cookie and then re-direct user

阅读更多
@app.route('/login', methods=['POST', 'GET'])
def login():
  errors = []

  if request.method == 'POST':

    email = request.form['email']
    password = request.form['password']

    #Check the user's e-mail
    try:
      u = User(email)
    except UserError, e:
      errors.append(e)
    else:

      #Check the user's password
      if not u.authenticatePassword(password):
        errors.append(('password','Invalid password'))
        return render_template('login.html',error=errors)

    #Set the session
    s = Session()
    s.user_id = u.user_id
    s.ip = request.remote_addr

    #Try to set the cookie
    if s.setSession():
      response = make_response( render_template('login.html',error=errors))
      response.set_cookie('session_id', s.session_id)
      return response

  return render_template('login.html',error=errors)


尴尬
if s.setSession():
    response = make_response(redirect('/home'))
    response.set_cookie('session_id', s.session_id)
    return response

my 

   redirect_to_index = redirect('/login')

   response = app.make_response(redirect_to_index )

   response.set_cookie('Cookies','fjdsafjafj9392jfn',max_age= 60 * 60)

   return  response

   # return redirect("htpp://www.baidu.com")

 

 

尴尬

 

 

resp = make_response(redirect('http://www.baidu.com'))
#resp = app.make_response(render_template('index.html',form=myForm))
#设置cookies有效期一个小时
resp.set_cookie('Cookies','fjdsafjafj9392jfn',max_age= 60 * 60,path='/')
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics