日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
創(chuàng)新互聯Flask教程:Flask會話

與Cookie不同,Session(會話)數據存儲在服務器上。會話是客戶端登錄到服務器并注銷服務器的時間間隔。需要在該會話中保存的數據會存儲在服務器上的臨時目錄中。

創(chuàng)新互聯建站從2013年成立,先為宿豫等服務建站,宿豫等地企業(yè),進行企業(yè)商務咨詢服務。為宿豫企業(yè)網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。

為每個客戶端的會話分配會話ID。會話數據存儲在cookie的頂部,服務器以加密方式對其進行簽名。對于此加密,Flask應用程序需要一個定義的SECRET_KEY。

Session對象也是一個字典對象,包含會話變量和關聯值的鍵值對。

例如,要設置一個'username'會話變量,請使用以下語句:

Session['username'] = 'admin'

要釋放會話變量,請使用pop()方法。

session.pop('username', None)

以下代碼是Flask中的會話工作的簡單演示。URL '/'只是提示用戶登錄,因為未設置會話變量'username'。

@app.route('/')
def index():
   if 'username' in session:
      username = session['username']
         return 'Logged in as ' + username + '
' \ "click here to log out" return "You are not logged in
" + \ "click here to log in"

當用戶瀏覽到“/login”login()視圖函數時,因為它是通過GET方法調用的,所以將打開一個登錄表單。

表單發(fā)送回'/login',現在會話變量已設置。應用程序重定向到'/'。此時會話變量'username'被找到。

@app.route('/login', methods = ['GET', 'POST'])
def login():
   if request.method == 'POST':
      session['username'] = request.form['username']
      return redirect(url_for('index'))
   return '''
	
   

'''

應用程序還包含一個logout()視圖函數,它會彈出'username'會話變量。因此,'/' URL再次顯示開始頁面。

@app.route('/logout')
def logout():
   # remove the username from the session if it is there
   session.pop('username', None)
   return redirect(url_for('index'))

運行應用程序并訪問主頁。(確保設置應用程序的secret_key

from flask import Flask, session, redirect, url_for, escape, request
app = Flask(__name__)
app.secret_key = 'any random string’

完整代碼如下所示

from flask import render_template

from flask import make_response

from flask import Flask, session, redirect, url_for, escape, request

app = Flask(__name__)

app.secret_key = 'fkdjsafjdkfdlkjfadskjfadskljdsfklj'

@app.route('/')

def index():

    if 'username' in session:

        username = session['username']

        return '登錄用戶名是:' + username + '
' + \

                 "點擊這里注銷"

    return "您暫未登錄, 
" + \

         "點擊這里登錄"

@app.route('/login', methods = ['GET', 'POST'])

def login():

    if request.method == 'POST':

        session['username'] = request.form['username']

        return redirect(url_for('index'))

    return '''

   

      

      

   

   '''

@app.route('/logout')

def logout():

   # remove the username from the session if it is there

   session.pop('username', None)

   return redirect(url_for('index'))

if __name__ == '__main__':

    app.run(debug = True)

輸出將顯示如下。點擊“點擊此處登錄”鏈接。

鏈接將被定向到另一個屏幕。鍵入“admin”。

屏幕會顯示消息“ 登錄用戶名是:admin ”


網頁名稱:創(chuàng)新互聯Flask教程:Flask會話
網站URL:http://www.5511xx.com/article/cdcsees.html