<h1 id="autoid-0-0-0">cookie
<h3 id="autoid-0-1-0">Cookie的由来
大家都知道HTTP协议是无状态的。
无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不会直接影响后面的请求响应情况。
一句有意思的话来描述就是人生只如初见,对服务器来说,每次的请求都是全新的。
状态可以理解为客户端和服务器在某次会话中产生的数据,那无状态的就以为这些数据不会被保留。会话中产生的数据又是我们需要保存的,也就是说要“保持状态”。因此Cookie就是在这样一个场景下诞生。
Cookie具体指的是一段小信息,它是服务器发送出来存储在浏览器上的一组组键值对,下次访问服务器时浏览器会自动携带这些键值对,以便服务器提取有用信息。
cookie的工作原理是:由服务器产生内容,浏览器收到请求后保存在本地;当浏览器再次访问时,浏览器会自动带上Cookie,这样服务器就能通过Cookie的内容来判断这个是“谁”了。
查看Cookie
我们使用Chrome浏览器,打开开发者工具。

?
request.COOKIES[=RAISE_ERROR,salt=,max_age=None)
参数:
- default: 默认值
- salt: 加密盐
- max_age: 后台控制过期时间
rep =rep.set_cookie(key,value,...)
rep.set_signed_cookie(key,salt=<span style="color: #800000;">'<span style="color: #800000;">加密盐<span style="color: #800000;">',max_age=None,...)
参数:
- key,键
- value='',值
- max_age=None,超时时间
- expires=None,超时时间(IE requires expires,so set it if hasn't been already.)
- path='/',Cookie生效的路径,/ 表示根路径,特殊的:根路径的cookie可以被任何url的页面访问
- domain=None,Cookie生效的域名
- secure=False,https传输
- httponly=False 只能http协议传输,无法被JavaScript获取(不是绝对,底层抓包可以获取到也可以被覆盖)
= redirect()
rep
Cookie版登陆校验
inner(request,*args,**= request.get_signed_cookie(,salt=,default=None) ==
func(request,**
redirect(<span style="color: #0000ff;">def<span style="color: #000000;"> login(request):
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">"<span style="color: #800000;">POST<span style="color: #800000;">"<span style="color: #000000;">:
username = request.POST.get(<span style="color: #800000;">"<span style="color: #800000;">username<span style="color: #800000;">"<span style="color: #000000;">)
passwd = request.POST.get(<span style="color: #800000;">"<span style="color: #800000;">password<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">if username == <span style="color: #800000;">"<span style="color: #800000;">xxx<span style="color: #800000;">" <span style="color: #0000ff;">and passwd == <span style="color: #800000;">"<span style="color: #800000;">dashabi<span style="color: #800000;">"<span style="color: #000000;">:
next_url = request.GET.get(<span style="color: #800000;">"<span style="color: #800000;">next<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">if next_url <span style="color: #0000ff;">and next_url != <span style="color: #800000;">"<span style="color: #800000;">/logout/<span style="color: #800000;">"<span style="color: #000000;">:
response =<span style="color: #000000;"> redirect(next_url)
<span style="color: #0000ff;">else<span style="color: #000000;">:
response = redirect(<span style="color: #800000;">"<span style="color: #800000;">/class_list/<span style="color: #800000;">"<span style="color: #000000;">)
response.set_signed_cookie(<span style="color: #800000;">"<span style="color: #800000;">login<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">yes<span style="color: #800000;">",salt=<span style="color: #800000;">"<span style="color: #800000;">SSS<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">return<span style="color: #000000;"> response
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">"<span style="color: #800000;">login.html<span style="color: #800000;">")
Session
Session的由来
Cookie虽然在一定程度上解决了“保持状态”的需求,但是由于Cookie本身最大支持4096字节,以及Cookie本身保存在客户端,可能被拦截或窃取,因此就需要有一种新的东西,它能支持更多的字节,并且他保存在服务器,有较高的安全性。这就是Session。
问题来了,基于HTTP协议的无状态特征,服务器根本就不知道访问者是“谁”。那么上述的Cookie就起到桥接的作用。
我们可以给每个客户端的Cookie分配一个唯一的id,这样用户在访问时,通过Cookie,服务器就知道来的人是“谁”。然后我们再根据不同的Cookie的id,在服务器上保存一段时间的私密资料,如“账号密码”等等。
总结而言:Cookie弥补了HTTP无状态的不足,让服务器知道来的人是“谁”;但是Cookie以文本的形式保存在本地,自身安全性较差;所以我们就通过Cookie识别不同的用户,对应的在Session里保存私密的信息以及超过4096字节的文本。
另外,上述所说的Cookie和Session其实是共通性的东西,不限于语言和框架。
request.session[] = 123,123)
request.session[<span style="color: #008000;">#<span style="color: #008000;"> 所有 键、值、键值对
<span style="color: #000000;">request.session.keys()
request.session.values()
request.session.items()
request.session.iterkeys()
request.session.itervalues()
request.session.iteritems()
<span style="color: #008000;">#<span style="color: #008000;"> 会话session的key
<span style="color: #000000;">request.session.session_key
<span style="color: #008000;">#<span style="color: #008000;"> 将所有Session失效日期小于当前日期的数据删除
<span style="color: #000000;">request.session.clear_expired()
<span style="color: #008000;">#<span style="color: #008000;"> 检查会话session的key在数据库中是否存在
request.session.exists(<span style="color: #800000;">"<span style="color: #800000;">session_key<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;"> 删除当前会话的所有Session数据
<span style="color: #000000;">request.session.delete()
<span style="color: #008000;">#<span style="color: #008000;"> 删除当前的会话数据并删除会话的Cookie。
<span style="color: #000000;">request.session.flush()
这用于确保前面的会话数据不可以再次被用户的浏览器访问
例如,django.contrib.auth.logout() 函数中就会调用它。
<span style="color: #008000;">#<span style="color: #008000;"> 设置会话Session和Cookie的超时时间
<span style="color: #000000;">request.session.set_expiry(value)
<span style="color: #000000;"> 如果value是个整数,session会在些秒数后失效。
<span style="color: #000000;"> 如果value是个datatime或timedelta,session就会在这个时间后失效。
<span style="color: #000000;"> 如果value是0,用户关闭浏览器session就会失效。
如果value是None,session会依赖全局session失效策略。

functools <span style="color: #0000ff;">def<span style="color: #000000;"> check_login(func):
@wraps(func)
<span style="color: #0000ff;">def inner(request, <span style="color: #000000;">kwargs):
next_url =<span style="color: #000000;"> request.get_full_path()
<span style="color: #0000ff;">if request.session.get(<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"<span style="color: #000000;">):
<span style="color: #0000ff;">return func(request,<span style="color: #000000;">kwargs)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return redirect(<span style="color: #800000;">"<span style="color: #800000;">/login/?next={}<span style="color: #800000;">"<span style="color: #000000;">.format(next_url))
<span style="color: #0000ff;">return<span style="color: #000000;"> inner
<span style="color: #0000ff;">def<span style="color: #000000;"> login(request):
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">"<span style="color: #800000;">POST<span style="color: #800000;">"<span style="color: #000000;">:
user = request.POST.get(<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"<span style="color: #000000;">)
pwd = request.POST.get(<span style="color: #800000;">"<span style="color: #800000;">pwd<span style="color: #800000;">"<span style="color: #000000;">)
</span><span style="color: #0000ff;">if</span> user == <span style="color: #800000;">"</span><span style="color: #800000;">alex</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">and</span> pwd == <span style="color: #800000;">"</span><span style="color: #800000;">alex1234</span><span style="color: #800000;">"</span><span style="color: #000000;">:
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 设置session</span>
request.session[<span style="color: #800000;">"</span><span style="color: #800000;">user</span><span style="color: #800000;">"</span>] =<span style="color: #000000;"> user
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 获取跳到登陆页面之前的URL</span>
next_url = request.GET.get(<span style="color: #800000;">"</span><span style="color: #800000;">next</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 如果有,就跳转回登陆之前的URL</span>
<span style="color: #0000ff;">if</span><span style="color: #000000;"> next_url:
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> redirect(next_url)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 否则默认跳转到index页面</span>
<span style="color: #0000ff;">else</span><span style="color: #000000;">:
</span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">"</span><span style="color: #800000;">/index/</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">"</span><span style="color: #800000;">login.html</span><span style="color: #800000;">"</span><span style="color: #000000;">)
@check_login
<span style="color: #0000ff;">def<span style="color: #000000;"> logout(request):
<span style="color: #008000;">#<span style="color: #008000;"> 删除所有当前请求相关的session
<span style="color: #000000;"> request.session.delete()
<span style="color: #0000ff;">return redirect(<span style="color: #800000;">"<span style="color: #800000;">/login/<span style="color: #800000;">"<span style="color: #000000;">)
@check_login
<span style="color: #0000ff;">def<span style="color: #000000;"> index(request):
current_user = request.session.get(<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"<span style="color: #000000;">,None)
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">"<span style="color: #800000;">index.html<span style="color: #800000;">",{<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">": current_user})
Django中的Session配置
1 =
2<span style="color: #000000;">. 缓存Session
SESSION_ENGINE = <span style="color: #800000;">'<span style="color: #800000;">django.contrib.sessions.backends.cache<span style="color: #800000;">' <span style="color: #008000;">#<span style="color: #008000;"> 引擎
SESSION_CACHE_ALIAS = <span style="color: #800000;">'<span style="color: #800000;">default<span style="color: #800000;">' <span style="color: #008000;">#<span style="color: #008000;"> 使用的缓存别名(默认内存缓存,也可以是memcache),此处别名依赖缓存的设置
3<span style="color: #000000;">. 文件Session
SESSION_ENGINE = <span style="color: #800000;">'<span style="color: #800000;">django.contrib.sessions.backends.file<span style="color: #800000;">' <span style="color: #008000;">#<span style="color: #008000;"> 引擎
SESSION_FILE_PATH = None <span style="color: #008000;">#<span style="color: #008000;"> 缓存文件路径,如果为None,则使用tempfile模块获取一个临时地址tempfile.gettempdir()
- 缓存+<span style="color: #000000;">数据库
SESSION_ENGINE = <span style="color: #800000;">'<span style="color: #800000;">django.contrib.sessions.backends.cached_db<span style="color: #800000;">' <span style="color: #008000;">#<span style="color: #008000;"> 引擎
5<span style="color: #000000;">. 加密Cookie Session
SESSION_ENGINE = <span style="color: #800000;">'<span style="color: #800000;">django.contrib.sessions.backends.signed_cookies<span style="color: #800000;">' <span style="color: #008000;">#<span style="color: #008000;"> 引擎
<span style="color: #000000;">
其他公用设置项:
SESSION_COOKIE_NAME = <span style="color: #800000;">"<span style="color: #800000;">sessionid<span style="color: #800000;">" <span style="color: #008000;">#<span style="color: #008000;"> Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认)
SESSION_COOKIE_PATH = <span style="color: #800000;">"<span style="color: #800000;">/<span style="color: #800000;">" <span style="color: #008000;">#<span style="color: #008000;"> Session的cookie保存的路径(默认)
SESSION_COOKIE_DOMAIN = None <span style="color: #008000;">#<span style="color: #008000;"> Session的cookie保存的域名(默认)
SESSION_COOKIE_SECURE = False <span style="color: #008000;">#<span style="color: #008000;"> 是否Https传输cookie(默认)
SESSION_COOKIE_HTTPONLY = True <span style="color: #008000;">#<span style="color: #008000;"> 是否Session的cookie只支持http传输(默认)
SESSION_COOKIE_AGE = 1209600 <span style="color: #008000;">#<span style="color: #008000;"> Session的cookie失效日期(2周)(默认)
SESSION_EXPIRE_AT_BROWSER_CLOSE = False <span style="color: #008000;">#<span style="color: #008000;"> 是否关闭浏览器使得Session过期(默认)
SESSION_SAVE_EVERY_REQUEST = False <span style="color: #008000;">#<span style="color: #008000;"> 是否每次请求都保存Session,默认修改之后才保存(默认)
CBV中加装饰器相关
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> get(self,request):
</span><span style="color: #800000;">"""</span><span style="color: #800000;">
处理GET请求
</span><span style="color: #800000;">"""</span>
<span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">login.html</span><span style="color: #800000;">'</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> post(self,request):
</span><span style="color: #800000;">"""</span><span style="color: #800000;">
处理POST请求
</span><span style="color: #800000;">"""</span><span style="color: #000000;">
user </span>= request.POST.get(<span style="color: #800000;">'</span><span style="color: #800000;">user</span><span style="color: #800000;">'</span><span style="color: #000000;">)
pwd </span>= request.POST.get(<span style="color: #800000;">'</span><span style="color: #800000;">pwd</span><span style="color: #800000;">'</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">if</span> user == <span style="color: #800000;">'</span><span style="color: #800000;">alex</span><span style="color: #800000;">'</span> <span style="color: #0000ff;">and</span> pwd == <span style="color: #800000;">"</span><span style="color: #800000;">alex1234</span><span style="color: #800000;">"</span><span style="color: #000000;">:
next_url </span>= request.GET.get(<span style="color: #800000;">"</span><span style="color: #800000;">next</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 生成随机字符串</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> 写浏览器cookie -> session_id: 随机字符串</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> 写到服务端session:</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> {</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> "随机字符串": {'user':'alex'}</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> }</span>
request.session[<span style="color: #800000;">'</span><span style="color: #800000;">user</span><span style="color: #800000;">'</span>] =<span style="color: #000000;"> user
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> next_url:
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> redirect(next_url)
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:
</span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">'</span><span style="color: #800000;">/index/</span><span style="color: #800000;">'</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">login.html</span><span style="color: #800000;">'</span>)</pre>
要在CBV视图中使用我们上面的check_login装饰器,有以下三种方式:
from django.utils.decorators import method_decorator
1. 加在CBV视图的get或post方法上
django.utils.decorators <span style="color: #0000ff;">class<span style="color: #000000;"> HomeView(View):
</span><span style="color: #0000ff;">def</span> dispatch(self,request,**<span style="color: #000000;">kwargs):
</span><span style="color: #0000ff;">return</span> super(HomeView,self).dispatch(request,**<span style="color: #000000;">kwargs)
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> get(self,request):
</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">"</span><span style="color: #800000;">home.html</span><span style="color: #800000;">"</span><span style="color: #000000;">)
@method_decorator(check_login)
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> post(self,request):
</span><span style="color: #0000ff;">print</span>(<span style="color: #800000;">"</span><span style="color: #800000;">Home View POST method...</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">"</span><span style="color: #800000;">/index/</span><span style="color: #800000;">"</span>)</pre>
2. 加在dispatch方法上
django.utils.decorators <span style="color: #0000ff;">class<span style="color: #000000;"> HomeView(View):
@method_decorator(check_login)
</span><span style="color: #0000ff;">def</span> dispatch(self,<span style="color: #800000;">"</span><span style="color: #800000;">home.html</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> post(self,request):
</span><span style="color: #0000ff;">print</span>(<span style="color: #800000;">"</span><span style="color: #800000;">Home View POST method...</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">"</span><span style="color: #800000;">/index/</span><span style="color: #800000;">"</span>)</pre>
因为CBV中首先执行的就是dispatch方法,所以这么写相当于给get和post方法都加上了登录校验。
3. 直接加在视图类上,但method_decorator必须传 name 关键字参数
如果get方法和post方法都需要登录校验的话就写两个装饰器。
django.utils.decorators @method_decorator(check_login,name=<span style="color: #800000;">"<span style="color: #800000;">get<span style="color: #800000;">"<span style="color: #000000;">)
@method_decorator(check_login,name=<span style="color: #800000;">"<span style="color: #800000;">post<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">class<span style="color: #000000;"> HomeView(View):
</span><span style="color: #0000ff;">def</span> dispatch(self,request):
</span><span style="color: #0000ff;">print</span>(<span style="color: #800000;">"</span><span style="color: #800000;">Home View POST method...</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">"</span><span style="color: #800000;">/index/</span><span style="color: #800000;">"</span>)</pre>
CSRF Token相关装饰器在CBV只能加到dispatch方法上,或者加在视图类上然后name参数指定为dispatch方法。
备注:
- csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
- csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
django.views.decorators.csrf django.utils.decorators <span style="color: #0000ff;">class<span style="color: #000000;"> HomeView(View):
@method_decorator(csrf_exempt)
</span><span style="color: #0000ff;">def</span> dispatch(self,request):
</span><span style="color: #0000ff;">print</span>(<span style="color: #800000;">"</span><span style="color: #800000;">Home View POST method...</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">"</span><span style="color: #800000;">/index/</span><span style="color: #800000;">"</span>)</pre>
或者
django.views.decorators.csrf django.utils.decorators @method_decorator(csrf_exempt,name=<span style="color: #800000;">'<span style="color: #800000;">dispatch<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">class<span style="color: #000000;"> HomeView(View):
</span><span style="color: #0000ff;">def</span> dispatch(self,request):
</span><span style="color: #0000ff;">print</span>(<span style="color: #800000;">"</span><span style="color: #800000;">Home View POST method...</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">"</span><span style="color: #800000;">/index/</span><span style="color: #800000;">"</span>)</pre>
分页
当数据库中数据有很多,我们通常会在前端页面做分页展示。
分页的数据可以在前端页面实现,也可以在后端实现分页。
后端实现分页的原理就是每次只请求一页数据。
准备工作
我们使用脚本批量创建一些测试数据(将下面的代码保存到bulk_create.py文件中放到Django项目的根目录,直接执行即可。):
<span style="color: #0000ff;">if <span style="color: #800080;"> name == <span style="color: #800000;">"<span style="color: #800000;"> main<span style="color: #800000;">"<span style="color: #000000;">:
os.environ.setdefault(<span style="color: #800000;">"<span style="color: #800000;">DJANGO_SETTINGS_MODULE<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">about_orm.settings<span style="color: #800000;">"<span style="color: #000000;">)
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> django
django.setup()
</span><span style="color: #0000ff;">from</span> app01 <span style="color: #0000ff;">import</span><span style="color: #000000;"> models
bulk_obj </span>= (models.Publisher(name=<span style="color: #800000;">'</span><span style="color: #800000;">沙河第{}出版社</span><span style="color: #800000;">'</span>.format(i)) <span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> range(300<span style="color: #000000;">))
models.Publisher.objects.bulk_create(bulk_obj)</span></pre>
自定义分页
= int(request.GET.get(
current_page = 1
total_count =
per_page = 10
total_page,more =+= 1
max_show = 11= max_show // 2
total_page <= max_show:
page_start = 1= current_page + half_show >= total_page:
page_end == total_page - current_page - half_show <= 1:
page_start = 1=:
page_start = current_page -= current_page +
data_start = (current_page-1) *= current_page *publisher_list </span>=<span style="color: #000000;"> models.Publisher.objects.all()[data_start:data_end]
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 生成页面上显示的页码</span>
page_html_list =<span style="color: #000000;"> []
page_html_list.append(</span><span style="color: #800000;">'</span><span style="color: #800000;"><nav aria-label="Page navigation"><ul class="pagination"></span><span style="color: #800000;">'</span><span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 加首页</span>
first_li = <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="/publisher_list/?page=1">首页</a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">
page_html_list.append(first_li)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 加上一页</span>
<span style="color: #0000ff;">if</span> current_page == 1<span style="color: #000000;">:
prev_li </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="#"><span aria-hidden="true">&laquo;</span></a></li></span><span style="color: #800000;">'</span>
<span style="color: #0000ff;">else</span><span style="color: #000000;">:
prev_li </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="/publisher_list/?page={}"><span aria-hidden="true">&laquo;</span></a></li></span><span style="color: #800000;">'</span>.format(current_page - 1<span style="color: #000000;">)
page_html_list.append(prev_li)
</span><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> range(page_start,page_end + 1<span style="color: #000000;">):
</span><span style="color: #0000ff;">if</span> i ==<span style="color: #000000;"> current_page:
li_tag </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li class="active"><a href="/publisher_list/?page={0}">{0}</a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(i)
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:
li_tag </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="/publisher_list/?page={0}">{0}</a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(i)
page_html_list.append(li_tag)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 加下一页</span>
<span style="color: #0000ff;">if</span> current_page ==<span style="color: #000000;"> total_page:
next_li </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="#"><span aria-hidden="true">&raquo;</span></a></li></span><span style="color: #800000;">'</span>
<span style="color: #0000ff;">else</span><span style="color: #000000;">:
next_li </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="/publisher_list/?page={}"><span aria-hidden="true">&raquo;</span></a></li></span><span style="color: #800000;">'</span>.format(current_page + 1<span style="color: #000000;">)
page_html_list.append(next_li)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 加尾页</span>
page_end_li = <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="/publisher_list/?page={}">尾页</a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(total_page)
page_html_list.append(page_end_li)
page_html_list.append(</span><span style="color: #800000;">'</span><span style="color: #800000;"></ul></nav></span><span style="color: #800000;">'</span><span style="color: #000000;">)
page_html </span>= <span style="color: #800000;">""</span><span style="color: #000000;">.join(page_html_list)
</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">"</span><span style="color: #800000;">publisher_list.html</span><span style="color: #800000;">"</span>,{<span style="color: #800000;">"</span><span style="color: #800000;">publisher_list</span><span style="color: #800000;">"</span>: publisher_list,<span style="color: #800000;">"</span><span style="color: #800000;">page_html</span><span style="color: #800000;">"</span>: page_html})</pre>
(self,current_page,total_count,base_url,per_page=10,max_show=11
=
self.current_page = 1
self.per_page =
total_page,per_page)
+= 1=
self.max_show == max_show // 2=@property
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> start(self):
</span><span style="color: #0000ff;">return</span> (self.current_page-1) *<span style="color: #000000;"> self.per_page
@property
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> end(self):
</span><span style="color: #0000ff;">return</span> self.current_page *<span style="color: #000000;"> self.per_page
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> page_html(self):
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 计算一下页面显示的页码范围</span>
<span style="color: #0000ff;">if</span> self.total_page <= self.max_show: <span style="color: #008000;">#</span><span style="color: #008000;"> 总页码数小于最大显示页码数</span>
page_start = 1<span style="color: #000000;">
page_end </span>=<span style="color: #000000;"> self.total_page
</span><span style="color: #0000ff;">elif</span> self.current_page + self.half_show >= self.total_page: <span style="color: #008000;">#</span><span style="color: #008000;"> 右边越界</span>
page_end =<span style="color: #000000;"> self.total_page
page_start </span>= self.total_page -<span style="color: #000000;"> self.max_show
</span><span style="color: #0000ff;">elif</span> self.current_page - self.half_show <= 1: <span style="color: #008000;">#</span><span style="color: #008000;"> 左边越界</span>
page_start = 1<span style="color: #000000;">
page_end </span>=<span style="color: #000000;"> self.max_show
</span><span style="color: #0000ff;">else</span>: <span style="color: #008000;">#</span><span style="color: #008000;"> 正常页码区间</span>
page_start = self.current_page -<span style="color: #000000;"> self.half_show
page_end </span>= self.current_page +<span style="color: #000000;"> self.half_show
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 生成页面上显示的页码</span>
page_html_list =<span style="color: #000000;"> []
page_html_list.append(</span><span style="color: #800000;">'</span><span style="color: #800000;"><nav aria-label="Page navigation"><ul class="pagination"></span><span style="color: #800000;">'</span><span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 加首页</span>
first_li = <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="{}?page=1">首页</a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(self.base_url)
page_html_list.append(first_li)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 加上一页</span>
<span style="color: #0000ff;">if</span> self.current_page == 1<span style="color: #000000;">:
prev_li </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="#"><span aria-hidden="true">&laquo;</span></a></li></span><span style="color: #800000;">'</span>
<span style="color: #0000ff;">else</span><span style="color: #000000;">:
prev_li </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="{}?page={}"><span aria-hidden="true">&laquo;</span></a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(
self.base_url,self.current_page </span>- 1<span style="color: #000000;">)
page_html_list.append(prev_li)
</span><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> range(page_start,page_end + 1<span style="color: #000000;">):
</span><span style="color: #0000ff;">if</span> i ==<span style="color: #000000;"> self.current_page:
li_tag </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li class="active"><a href="{0}?page={1}">{1}</a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(self.base_url,i)
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:
li_tag </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="{0}?page={1}">{1}</a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(self.base_url,i)
page_html_list.append(li_tag)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 加下一页</span>
<span style="color: #0000ff;">if</span> self.current_page ==<span style="color: #000000;"> self.total_page:
next_li </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="#"><span aria-hidden="true">&raquo;</span></a></li></span><span style="color: #800000;">'</span>
<span style="color: #0000ff;">else</span><span style="color: #000000;">:
next_li </span>= <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="{}?page={}"><span aria-hidden="true">&raquo;</span></a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(
self.base_url,self.current_page </span>+ 1<span style="color: #000000;">)
page_html_list.append(next_li)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 加尾页</span>
page_end_li = <span style="color: #800000;">'</span><span style="color: #800000;"><li><a href="{}?page={}">尾页</a></li></span><span style="color: #800000;">'</span><span style="color: #000000;">.format(self.base_url,self.total_page)
page_html_list.append(page_end_li)
page_html_list.append(</span><span style="color: #800000;">'</span><span style="color: #800000;"></ul></nav></span><span style="color: #800000;">'</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> <span style="color: #800000;">""</span>.join(page_html_list)</pre>
current_page = int(request.GET.get(
total_count ==== render(request,{: data,: page_html})
Django内置分页
django.shortcuts django.core.paginator L =<span style="color: #000000;"> []
<span style="color: #0000ff;">for i <span style="color: #0000ff;">in range(999<span style="color: #000000;">):
L.append(i)
<span style="color: #0000ff;">def<span style="color: #000000;"> index(request):
current_page = request.GET.get(<span style="color: #800000;">'<span style="color: #800000;">p<span style="color: #800000;">'<span style="color: #000000;">)
paginator </span>= Paginator(L,10<span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> per_page: 每页显示条目数量</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> count: 数据总个数</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> num_pages:总页数</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> page_range:总页数的索引范围,如: (1,10),(1,200)</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> page: page对象</span>
<span style="color: #0000ff;">try</span><span style="color: #000000;">:
posts </span>=<span style="color: #000000;"> paginator.page(current_page)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> has_next 是否有下一页</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> next_page_number 下一页页码</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> has_previous 是否有上一页</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> previous_page_number 上一页页码</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> object_list 分页之后的数据列表</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> number 当前页</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> paginator paginator对象</span>
<span style="color: #0000ff;">except</span><span style="color: #000000;"> PageNotAnInteger:
posts </span>= paginator.page(1<span style="color: #000000;">)
</span><span style="color: #0000ff;">except</span><span style="color: #000000;"> EmptyPage:
posts </span>=<span style="color: #000000;"> paginator.page(paginator.num_pages)
</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">index.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">'</span><span style="color: #800000;">posts</span><span style="color: #800000;">'</span>: posts})</pre>
{{ item }}
<span style="color: #0000ff;">< <span style="color: #800000;">div <span style="color: #ff0000;">class<span style="color: #0000ff;">="pagination"<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">span <span style="color: #ff0000;">class<span style="color: #0000ff;">="step-links"<span style="color: #0000ff;">><span style="color: #000000;">
{% if posts.has_previous %}
<span style="color: #0000ff;"><<span style="color: #800000;">a <span style="color: #ff0000;">href<span style="color: #0000ff;">="?p={{ posts.previous_page_number }}"<span style="color: #0000ff;">>Previous<span style="color: #0000ff;"></<span style="color: #800000;">a<span style="color: #0000ff;">><span style="color: #000000;">
{% endif %}
<span style="color: #0000ff;"><<span style="color: #800000;">span <span style="color: #ff0000;">class<span style="color: #0000ff;">="current"<span style="color: #0000ff;">><span style="color: #000000;">
Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
<span style="color: #0000ff;"></<span style="color: #800000;">span<span style="color: #0000ff;">><span style="color: #000000;">
{% if posts.has_next %}
<span style="color: #0000ff;"><<span style="color: #800000;">a <span style="color: #ff0000;">href<span style="color: #0000ff;">="?p={{ posts.next_page_number }}"<span style="color: #0000ff;">>Next<span style="color: #0000ff;"></<span style="color: #800000;">a<span style="color: #0000ff;">><span style="color: #000000;">
{% endif %}
<span style="color: #0000ff;"></<span style="color: #800000;">span<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">div<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">body<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">html<span style="color: #0000ff;">>
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|