urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index, name='index'),
]
views.py
# encoding=utf-8
from django.shortcuts import render
# Create your views here.
def index(request):
#模拟数据,列表生成式
a_list = [i for i in range(1, 105)]
#获取当前页, 注意获取时是字符串,要转换为int
current_page = request.GET.get('p', 1)
current_page = int(current_page)
#数据切片
#data = a_list[:10]
#data = a_list[20: 30]
#每页显示10条数据,并且得与current_page发生关系,每页10条数据
start = (current_page-1) * 10
end = current_page * 10
data = a_list[start: end]
return render(request, 'index.html', {'data': data})
template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul>
{% for d in data %}
<li>{{ d }}</li>
{% endfor %}
</ul>
</body>
</html>
这样127.0.0.1:8000/index/?p=1
127.0.0.1:8000/index/?p=2
127.0.0.1:8000/index/?p=3
.....
随着页数变化,每页也发生变化。