from django.shortcuts import render,HttpResponse
from app01 import models
#
# def user_list(request):
# for i in range(500):
# dic = {'name':'name_%d' % i, 'age':i}
# models.User.objects.create(**dic)
# return HttpResponse('OK')
#
#
def user_list(request):
current_page = request.GET.get('page',1)
print(current_page)
current_page = int(current_page)
start1 = (current_page-1)*10
end1 = current_page*10
all_item = models.User.objects.all().count()
all_page ,div = divmod(all_item, 10)
if div > 0:
all_page +=1
page_str = ''
if all_page <=11:
start = 1
end = all_page
else:
if current_page <=6:
start = 1
end = 11 + 1
else:
start = current_page - 5
end = current_page + 6
if current_page + 6 > all_page:
start = all_page - 10
end = all_page + 1
for i in range(start,end):
if i == current_page:
temp = '<a style="color:red; font-size:26px;padding: 5px" href="/user_list/?page=%d">%d</a>'% (i,i)
else:
temp = '<a href="/user_list/?page=%d">%d</a>' % (i, i)
page_str += temp
user_list = models.User.objects.all()[start1:end1]
return render(request,'user_list.html',{'user_list':user_list, 'page_str':page_str})