- 在某一个app文件下创建一个model.py的文件添加如下内容
from django.db import models
# Create your models here.
class BlogArticle(models.Model):
title = models.CharField(max_length= 50)
author = models.CharField(max_length= 20)
time = models.IntegerField(default = 0)
进入到项目文件夹下面使用命令
python manage.py makemigrations
python manage.py migrate
分页与数据结构无关
下面是一个简单的分页测试
html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for article in page_obj %}
<div>## {{ article }} ##</div>
{% endfor %}
{% for page_num in paginator_obj.page_range %}
<a href="?page={{ page_num }}">{{ page_num }}</a>
{% endfor %}
</body>
</html>
views.py
def page_demo(request):
lista=[]
for i in range(1, 96):
title = "title" + str(i)
content = "text" + str(i)
lista.append(content)
paginator_obj = Paginator(lista, 5)
request_page_num = request.GET.get('page', 1)
page_obj = paginator_obj.page(request_page_num)
total_page_number = paginator_obj.num_pages
return render(request, 'page_demo.html', {'page_obj': page_obj, 'paginator_obj': paginator_obj})