@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='/')
标签:errors,return,Flask,direct,response,set,cookie,id
From: https://blog.51cto.com/u_6186189/6235734