Flask用户会话

时间:2018-06-05 17:31:17

标签: python flask flask-login flask-session

在对用户进行身份验证后,我遇到了让用户在浏览网站时登录的问题。我尝试过使用cookie和会话,但我似乎无法使用它。

我正在共享我的登录代码,我从抓取数据中通过注册时填充的Excel工作表验证用户

我找到每个团队成员的名字,让他们出现在团队页面上,这是登录后用户被重定向到的地方

total = tf.add_n(tensors)

这是用户身份验证的地方

@app.route('/login',methods=['POST','GET'])
def login():
# get data from form
try:
    username = str(request.form['username2'])
    password2 = str(request.form['password2'])
    teamname = str(request.form['teamname'])
    print(username)
    print(password2)

    # read excel sheet
    data = pd.read_excel("C:\\Users\\290002925\\Documents\\registration.xlsx")

    for name in data['Team']:
        if name == teamname:
            team_info = data.loc[data['Team'] == teamname]
            print(team_info)
            team_info['FullName'] = team_info['FirstName'] + [' '] + team_info['LastName']
            teamroster = team_info[['FullName']].copy()
            finalteamroster = pd.DataFrame(teamroster, index=range(5))
            print(finalteamroster)
            print(len(finalteamroster))
            finalteamroster.fillna(value='', inplace=True)
            print(finalteamroster)

            amember1 = finalteamroster.at[0,'FullName']
            print(amember1)
            amember2 = finalteamroster.at[1,'FullName']
            print(amember2)
            amember3 = finalteamroster.at[2,'FullName']
            print(amember3)
            amember4 = finalteamroster.at[3,'FullName']
            print(amember4)
            amember5 = finalteamroster.at[4,'FullName']
            print(amember5)

这是我写出 cookies 的方式,但我认为会话可能更有用

    for user in data['Email']:
        #check username
        if user == username:
            passw = data.loc[data['Email'] == username]
                # check password
            print(passw)
            test2 = passw.iat[0,3]

Teampage代码

            if test2 == password2:
                    # authenticated
                print("User is authenticated")
                response = render_template('teampage.html', teamname=teamname, member1=amember1, member2=amember2, member3=amember3, member4=amember4, member5=amember5)
                response.set_cookie('userID', username)
                return response

            else:
                print("Password is incorrect")
                return render_template('login.html')
        else:
            print("Username is incorrect")
        return render_template('index.html')

except:
    print("fail")
return render_template('login.html')

HTML用户参考

@app.route('/teampage')
def teampage():
    userid = request.cookies.get('userID')
    return render_template('teampage.html', userid=userid)

1 个答案:

答案 0 :(得分:0)

理解您的初始代码有点难,但我理解的是您需要维护用户会话 -

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
    return render_template('login.html', title=': Login')
if request.method == 'POST':
   if YourMethodToCheckPassword==True:
            session['username'] = request.form['user']
            return redirect(url_for('home'))

然后使用Session对象,比如 -

@app.route('/home')
def home():
    if 'username' in session:
        return render_template("index.html", name=session['username'])

在HTML中使用

{{ name }}