#主界面视图函数
@app.route('/index')
def index():
# 获取cookie
username = request.cookies.get("name")
return render_template('index.html', username=username)
#登录界面视图函数
@app.route('/login', methods=['GET', 'POST'])
def login():
session = DBSession()
if request.method == 'GET':
return render_template('login.html')
else:
username = request.form.get('login')
pw = request.form.get('login')
user = session.query(User).filter(User.username == username).first()
if user:
if session.query(User).filter(User.pw == pw).first():
# cookie设置
response = make_response(redirect(url_for('index', username=username)))
response.set_cookie(key="name",value=username)
return response
else:
flash('密码错误 !')
return redirect(url_for('login'))
else:
flash('用户名不存在 !')
return redirect(url_for('login'))