目录
一、Cookie
二、Session
一、Cookie
1.获取Cookie
request.COOKIES["key"] request.get_signed_cookie(key, default=RAISE_ERROR, s, max_age=None) # 参数 # default:默认值 # salt:加密盐 # max_age:后台控制过期时间
2.设置Cookie
rep = HttpResponse(...) 或 rep = render(request, ...) rep.set_cookie(key, value, ...) rep.set_signed_cookie(key, value, salt="加密盐", ...) # 如果只有key、value默认关闭浏览器后cookie就失效了,这里可以设置以下一些参数来自定义 # 参数 # key, 键 # value="", 值 # max_age=None, 超时时间,默认秒 # expires=None, 超时时间,到什么时候截止 # path="/", Cookie生效的路径,/表示跟路径,特殊的:跟路径的cookie可以被任何url的页面访问 # domain=None, Cookie生效的域名 # secure=False, https传输 # httponly=False, 只能http协议传输,无法被JavaScript获取(不是绝对,底层抓包可以获取到也可以被覆盖)
由于cookie保存在客户端的电脑上,所以,JavaScript和jQuery也可以操作cookie
<script src="/static/js/jquery.cookie.js"></script> $.cookie("list_pager_num", 30, {path:"/"});
通过cookie来实现用户登录
urls.py
from django.conf.urls import url from django.contrib import admin from django.conf.urls import include from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^login/‘, views.login), url(r‘^index/‘, views.index), ]
views.py
user_info = { "dachengzi" : {"pwd": "123123"}, "kangbazi": {"pwd": "kkkkkk"}, } def login(request): if request.method == "GET": return render(request, "login.html") if request.method == "POST": u = request.POST.get("username") p = request.POST.get("password") dic = user_info.get(u) if not dic: return render(request, "login.html") if dic["pwd"] == p: res = redirect("/index/") res.set_cookie("username", u) return res else: return render(request, "login.html") def index(request): # 获取当前已经登录的用户名 v = request.COOKIES.get("username") if not v: return redirect("/login") return render(request, "index.html", {"current_user": v})
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login/" method="POST"> <input type="text" name="username" placeholder="用户名"/> <input type="password" name="password" placeholder="密码"/> <input type="submit" /> </form> </body> </html>
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>欢迎登录,{{ current_user }}</h1> </body> </html>
通过cookie实现定制显示数据条目
这里引入了一个jQuery的插件
jquery.cookie.js
操作
<!--设置cookie--> $.cookie("per_page_count", v, {path: "/user_list/"}); <!--获取cookie--> $.cookie("per_page_count");
urls.py
from django.conf.urls import url from django.contrib import admin from django.conf.urls import include from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^user_list/‘, views.user_list), ]
views.py
from django.shortcuts import render from django.shortcuts import HttpResponse from django.shortcuts import redirect # Create your views here. from django.urls import reverse from utils import pagination LIST = [] for i in range(1009): LIST.append(i) def user_list(request): current_page = request.GET.get("p", 1) current_page = int(current_page) val = request.COOKIES.get("per_page_count") print(val) if val: val = int(val) page_obj = pagination.Page(current_page, len(LIST), val) data = LIST[page_obj.start:page_obj.end] page_str = page_obj.page_str("/user_list/") return render(request, "user_list.html", {"li": data, "page_str": page_str}) else: page_obj = pagination.Page(current_page, len(LIST)) data = LIST[page_obj.start:page_obj.end] page_str = page_obj.page_str("/user_list/") rep = render(request, "user_list.html", {"li": data, "page_str": page_str}) rep.set_cookie("per_page_count", "10", path="/user_list/") return rep
user_list.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .pagination .page{ display: inline-block; padding: 5px; background-color: cyan; margin: 5px; } .pagination .page.active{ background-color: brown; color: black; } </style> </head> <body> <ul> {% for item in li %} {% include "li.html" %} {% endfor %} </ul> <div> <select id="px" onchange="changePageSize(this);"> <option value="10">10</option> <option value="30">30</option> <option value="50">50</option> <option value="100">100</option> </select> </div> <div class="pagination"> {{ page_str }} </div> <script src="/static/jquery-1.12.4.js"></script> <script src="/static/jquery.cookie.js"></script> <script> $(function () { var v = $.cookie("per_page_count"); $("#px").val(v); }); function changePageSize(ths) { var v = $(ths).val(); $.cookie("per_page_count", v, {path: "/user_list/"}); location.reload(); } </script> </body> </html>
本文出自 “八英里” 博客,请务必保留此出处http://5921271.blog.51cto.com/5911271/1929633
时间: 2024-11-02 19:55:40