加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Django:cookie

发布时间:2020-12-15 17:16:38 所属栏目:大数据 来源:网络整理
导读:一,Django中操作cookie 1,获取cookie request.COOKIES[ =RAISE_ERROR,salt= ,max_age=None) 参数: default: 默认值 salt: 加密盐 max_age: 后台控制过期时间 2,设置cookie rep = rep.set_cookie(key,value,...) rep.set_signed_cookie(key,salt =span st

一,Django中操作cookie

  1,获取cookie

request.COOKIES[=RAISE_ERROR,salt=,max_age=None)

  参数:

  •   default: 默认值
  •   salt: 加密盐
  •   max_age: 后台控制过期时间

  2,设置cookie

rep =rep.set_cookie(key,value,...)
rep.set_signed_cookie(key,salt
=<span style="color: #800000">'<span style="color: #800000">加密盐<span style="color: #800000">',...)

参数:

  • 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获取(不是绝对,底层抓包可以获取到也可以被覆盖

3,删除cookie

= redirect() rep
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">"<span style="color: #000000">)

cookie版登录

二、Django中Session相关方法

request.session[] = 123,123) request.session[</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 所有 键、值、键值对</span>

<span style="color: #000000"> request.session.keys()
request.session.values()
request.session.items()
request.session.iterkeys()
request.session.itervalues()
request.session.iteritems()

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 用户session的随机字符串</span>

<span style="color: #000000"> request.session.session_key

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 将所有Session失效日期小于当前日期的数据删除</span>

<span style="color: #000000"> request.session.clear_expired()

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 检查 用户session的随机字符串 在数据库中是否</span>
request.session.exists(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;session_key</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 删除当前用户的所有Session数据</span>

<span style="color: #000000"> request.session.delete()

request.session.set_expiry(value)
    </span>*<span style="color: #000000"&gt; 如果value是个整数,session会在些秒数后失效。
    </span>*<span style="color: #000000"&gt; 如果value是个datatime或timedelta,session就会在这个时间后失效。
    </span>*<span style="color: #000000"&gt; 如果value是0,用户关闭浏览器session就会失效。
    </span>* 如果value是None,session会依赖全局session失效策略。</pre>

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"&gt;if</span> user == <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;alex</span><span style="color: #800000"&gt;"</span> <span style="color: #0000ff"&gt;and</span> pwd == <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;alex1234</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;:
        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 设置session</span>
        request.session[<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;user</span><span style="color: #800000"&gt;"</span>] =<span style="color: #000000"&gt; user
        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 获取跳到登陆页面之前的URL</span>
        next_url = request.GET.get(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;next</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)
        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 如果有,就跳转回登陆之前的URL</span>
        <span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; next_url:
            </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; redirect(next_url)
        </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 否则默认跳转到index页面</span>
        <span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;:
            </span><span style="color: #0000ff"&gt;return</span> redirect(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/index/</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)
</span><span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;login.html</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)

@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">"<span style="color: #000000">: current_user})

Session版登录验证

2,Django中的session配置

Django中默认支持Session,其内部提供了5种类型的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()

  1. 缓存+<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,默认修改之后才保存(默认)

3,CBV中加装饰器

</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; get(self,request): </span><span style="color: #800000"&gt;"""</span><span style="color: #800000"&gt; 处理GET请求 </span><span style="color: #800000"&gt;"""</span> <span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;login.html</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;) </span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; post(self,request): </span><span style="color: #800000"&gt;"""</span><span style="color: #800000"&gt; 处理POST请求 </span><span style="color: #800000"&gt;"""</span><span style="color: #000000"&gt; user </span>= request.POST.get(<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;user</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;) pwd </span>= request.POST.get(<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;pwd</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;) </span><span style="color: #0000ff"&gt;if</span> user == <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;alex</span><span style="color: #800000"&gt;'</span> <span style="color: #0000ff"&gt;and</span> pwd == <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;alex1234</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;: next_url </span>= request.GET.get(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;next</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;) </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 生成随机字符串</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 写浏览器cookie -> session_id: 随机字符串</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 写到服务端session:</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; {</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; "随机字符串": {'user':'alex'}</span> <span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; }</span> request.session[<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;user</span><span style="color: #800000"&gt;'</span>] =<span style="color: #000000"&gt; user </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; next_url: </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; redirect(next_url) </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: </span><span style="color: #0000ff"&gt;return</span> redirect(<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;/index/</span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;) </span><span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;login.html</span><span style="color: #800000"&gt;'</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"&gt;def</span> dispatch(self,request,**<span style="color: #000000"&gt;kwargs):
    </span><span style="color: #0000ff"&gt;return</span> super(HomeView,self).dispatch(request,**<span style="color: #000000"&gt;kwargs)

</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; get(self,request):
    </span><span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;home.html</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)

@method_decorator(check_login)
</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; post(self,request):
    </span><span style="color: #0000ff"&gt;print</span>(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;Home View POST method...</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)
    </span><span style="color: #0000ff"&gt;return</span> redirect(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/index/</span><span style="color: #800000"&gt;"</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"&gt;def</span> dispatch(self,<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;home.html</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)

</span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; post(self,request):
    </span><span style="color: #0000ff"&gt;print</span>(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;Home View POST method...</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)
    </span><span style="color: #0000ff"&gt;return</span> redirect(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/index/</span><span style="color: #800000"&gt;"</span>)</pre>

因为CBV中首先执行的就是dispatch方法,所以这么写相当于给get和post方法都加上了登录校验。

3. 直接加在视图类上,但method_decorator必须传 name 关键字参数

如果get方法和post方法都需要登录校验的话就写两个装饰器。

from django.utils.decorators import method_decorator

@method_decorator(check_login,name="get")
@method_decorator(check_login,name="post")
class HomeView(View):

def dispatch(self,**kwargs):
    return super(HomeView,**kwargs)

def get(self,request):
    return render(request,"home.html")

def post(self,request):
    print("Home View POST method...")
    return redirect("/index/")</pre>

4,补充

CSRF Token相关装饰器在CBV只能加到dispatch方法上

备注:

  • csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
  • csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
from django.views.decorators.csrf import csrf_exempt,csrf_protect

class HomeView(View):

@method_decorator(csrf_exempt)
def dispatch(self,request):
    print("Home View POST method...")
    return redirect("/index/")</pre>

三,分页

data =<span style="color: #0000ff">for i <span style="color: #0000ff">in range(1,302<span style="color: #000000">):
tmp = {<span style="color: #800000">"<span style="color: #800000">id<span style="color: #800000">": i,<span style="color: #800000">"<span style="color: #800000">name<span style="color: #800000">": <span style="color: #800000">"<span style="color: #800000">alex-{}<span style="color: #800000">"<span style="color: #000000">.format(i)}
data.append(tmp)

<span style="color: #0000ff">print<span style="color: #000000">(data)

<span style="color: #0000ff">def<span style="color: #000000"> user_list(request):

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; user_list = data[0:10]</span>
<span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; user_list = data[10:20]</span>
<span style="color: #0000ff"&gt;try</span><span style="color: #000000"&gt;:
    current_page </span>= int(request.GET.get(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;page</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;))
</span><span style="color: #0000ff"&gt;except</span><span style="color: #000000"&gt; Exception as e:
    current_page </span>= 1<span style="color: #000000"&gt;

per_page </span>= 10

<span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 数据总条数</span>
total_count =<span style="color: #000000"&gt; len(data)
</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 总页码</span>
total_page,more =<span style="color: #000000"&gt; divmod(total_count,per_page)
</span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; more:
    total_page </span>+= 1

<span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 页面最多显示多少个页码</span>
max_show = 11<span style="color: #000000"&gt;
half_show </span>= int((max_show-1)/2<span style="color: #000000"&gt;)

</span><span style="color: #0000ff"&gt;if</span> current_page <=<span style="color: #000000"&gt; half_show:
    show_start </span>= 1<span style="color: #000000"&gt;
    show_end </span>=<span style="color: #000000"&gt; max_show
</span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;:
    </span><span style="color: #0000ff"&gt;if</span> current_page + half_show >=<span style="color: #000000"&gt; total_page:
        show_start </span>= total_page -<span style="color: #000000"&gt; max_show
        show_end </span>=<span style="color: #000000"&gt; total_page
    </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;:
        show_start </span>= current_page -<span style="color: #000000"&gt; half_show
        show_end </span>= current_page +<span style="color: #000000"&gt; half_show

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 数据库中获取数据</span>
data_start = (current_page - 1) *<span style="color: #000000"&gt; per_page
data_end </span>= current_page *<span style="color: #000000"&gt; per_page

user_list </span>=<span style="color: #000000"&gt; data[data_start:data_end]

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 生成页面上显示的页码</span>
page_html_list =<span style="color: #000000"&gt; []
</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加首页</span>
first_li = <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="/user_list/?page=1"&gt;首页</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;
page_html_list.append(first_li)
</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加上一页</span>
<span style="color: #0000ff"&gt;if</span> current_page == 1<span style="color: #000000"&gt;:
    prev_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="#"&gt;上一页</a></li></span><span style="color: #800000"&gt;'</span>
<span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;:
    prev_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="/user_list/?page={}"&gt;上一页</a></li></span><span style="color: #800000"&gt;'</span>.format(current_page - 1<span style="color: #000000"&gt;)
page_html_list.append(prev_li)
</span><span style="color: #0000ff"&gt;for</span> i <span style="color: #0000ff"&gt;in</span> range(show_start,show_end+1<span style="color: #000000"&gt;):
    </span><span style="color: #0000ff"&gt;if</span> i ==<span style="color: #000000"&gt; current_page:
        li_tag </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li class="active"&gt;<a href="/user_list/?page={0}"&gt;{0}</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(i)
    </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;:
        li_tag </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="/user_list/?page={0}"&gt;{0}</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(i)
    page_html_list.append(li_tag)

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加下一页</span>
<span style="color: #0000ff"&gt;if</span> current_page ==<span style="color: #000000"&gt; total_page:
    next_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="#"&gt;下一页</a></li></span><span style="color: #800000"&gt;'</span>
<span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;:
    next_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="/user_list/?page={}"&gt;下一页</a></li></span><span style="color: #800000"&gt;'</span>.format(current_page+1<span style="color: #000000"&gt;)
page_html_list.append(next_li)

</span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加尾页</span>
page_end_li = <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="/user_list/?page={}"&gt;尾页</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(total_page)
page_html_list.append(page_end_li)

page_html </span>= <span style="color: #800000"&gt;""</span><span style="color: #000000"&gt;.join(page_html_list)

</span><span style="color: #0000ff"&gt;return</span> render(request,<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;user_list.html</span><span style="color: #800000"&gt;"</span>,{<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;user_list</span><span style="color: #800000"&gt;"</span>: user_list,<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;page_html</span><span style="color: #800000"&gt;"</span>: page_html})</pre>
(self,current_page,total_count,base_url,per_page=10,max_show=11 == 1 self.current_page </span>=<span style="color: #000000"&gt; current_page self.total_count </span>=<span style="color: #000000"&gt; total_count self.base_url </span>=<span style="color: #000000"&gt; base_url self.per_page </span>=<span style="color: #000000"&gt; per_page self.max_show </span>=<span style="color: #000000"&gt; max_show </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 总页码</span> total_page,per_page) </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; more: total_page </span>+= 1<span style="color: #000000"&gt; half_show </span>= int((max_show - 1) / 2<span style="color: #000000"&gt;) self.half_show </span>=<span style="color: #000000"&gt; half_show self.total_page </span>=<span style="color: #000000"&gt; total_page @property </span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; start(self): </span><span style="color: #0000ff"&gt;return</span> (self.current_page - 1) *<span style="color: #000000"&gt; self.per_page @property </span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; end(self): </span><span style="color: #0000ff"&gt;return</span> self.current_page *<span style="color: #000000"&gt; self.per_page </span><span style="color: #0000ff"&gt;def</span><span style="color: #000000"&gt; page_html(self): </span><span style="color: #0000ff"&gt;if</span> self.current_page <=<span style="color: #000000"&gt; self.half_show: show_start </span>= 1<span style="color: #000000"&gt; show_end </span>=<span style="color: #000000"&gt; self.max_show </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: </span><span style="color: #0000ff"&gt;if</span> self.current_page + self.half_show >=<span style="color: #000000"&gt; self.total_page: show_start </span>= self.total_page -<span style="color: #000000"&gt; self.max_show show_end </span>=<span style="color: #000000"&gt; self.total_page </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: show_start </span>= self.current_page -<span style="color: #000000"&gt; self.half_show show_end </span>= self.current_page +<span style="color: #000000"&gt; self.half_show </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 生成页面上显示的页码</span> page_html_list =<span style="color: #000000"&gt; [] </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加首页</span> first_li = <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{}?page=1"&gt;首页</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(self.base_url) page_html_list.append(first_li) </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加上一页</span> <span style="color: #0000ff"&gt;if</span> self.current_page == 1<span style="color: #000000"&gt;: prev_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="#"&gt;上一页</a></li></span><span style="color: #800000"&gt;'</span> <span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: prev_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{0}?page={1}"&gt;上一页</a></li></span><span style="color: #800000"&gt;'</span>.format(self.base_url,self.current_page - 1<span style="color: #000000"&gt;) page_html_list.append(prev_li) </span><span style="color: #0000ff"&gt;for</span> i <span style="color: #0000ff"&gt;in</span> range(show_start,show_end + 1<span style="color: #000000"&gt;): </span><span style="color: #0000ff"&gt;if</span> i ==<span style="color: #000000"&gt; self.current_page: li_tag </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li class="active"&gt;<a href="{0}?page={1}"&gt;{1}</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(self.base_url,i) </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: li_tag </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{0}?page={1}"&gt;{1}</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(self.base_url,i) page_html_list.append(li_tag) </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加下一页</span> <span style="color: #0000ff"&gt;if</span> self.current_page ==<span style="color: #000000"&gt; self.total_page: next_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="#"&gt;下一页</a></li></span><span style="color: #800000"&gt;'</span> <span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;: next_li </span>= <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{0}?page={1}"&gt;下一页</a></li></span><span style="color: #800000"&gt;'</span>.format(self.base_url,self.current_page + 1<span style="color: #000000"&gt;) page_html_list.append(next_li) </span><span style="color: #008000"&gt;#</span><span style="color: #008000"&gt; 加尾页</span> page_end_li = <span style="color: #800000"&gt;'</span><span style="color: #800000"&gt;<li><a href="{0}?page={1}"&gt;尾页</a></li></span><span style="color: #800000"&gt;'</span><span style="color: #000000"&gt;.format(self.base_url,self.total_page) page_html_list.append(page_end_li) </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800000"&gt;""</span>.join(page_html_list)</pre>

使用:

def user_list(request):
    pager = Pagination(request.GET.get("page"),len(data),request.path_info)
    user_list = data[pager.start:pager.end]
    page_html = pager.page_html()
    return render(request,"user_list.html",{"user_list": user_list,"page_html": page_html})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读