django setting session and remember user name

Reference
session is a commonplace problem, then django how to set up the session, its use here only briefly, examples are as follows:

session settings:

# session 设置
SESSION_COOKIE_AGE = 60 * 30  # 30分钟后失效 默认14天
SESSION_SAVE_EVERY_REQUEST = True  #每次请求都保存Session
SESSION_EXPIRE_AT_BROWSER_CLOSE = True  # 关闭浏览器,则COOKIE失效

Into the browser (the author uses Google) in Settings → Advanced → Site Settings → cookie and site data → see all the data found in the url
shown:
Here Insert Picture Description

SESSION_EXPIRE_AT_BROWSER_CLOSE #这个就是到期时间中:关闭浏览器结束会话,cookies失效。
session生存时间30分钟,如果没有使用session,也就是没有操作界面的话,30分钟后自动失效。

Remember username settings :

 # 判断是否记住用户名
            if remember == "on":
                # 设置cookie username *过期时间为1周,按秒计算
                response.set_cookie('username', username, max_age=7 * 24 * 3600)
            return response

FIG:
Here Insert Picture Description
As can be seen from the figure, just as the code set (7 * 24 * 3600) for 7 days.

Published 82 original articles · won praise 43 · views 180 000 +

Guess you like

Origin blog.csdn.net/liudinglong1989/article/details/104272654