标签:number img led dmi return -o 页码 print turn
效果:
url.py
main-urls
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('book/',include('book.urls'))
]
app-> book.urls
from django.urls import path
from . import views
app_name = 'book'
urlpatterns = [
path('add/',views.add_article,name='add_article'),
path('list/',views.ArticleListView.as_view(),name='article_list')
]
模型的创建
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
create_time = models.DateTimeField(auto_now_add=True)
添加数据:
def add_article(request):
articles = []
for x in range(100):
article = Article(title='标题 %s'%x,content = "内容 %s"%x)
articles.append(article)
Article.objects.bulk_create(articles) #一次性插入。传递一个列表
return HttpResponse('添加成功')
context:
def get_context_data(self, **kwargs):
context = super(ArticleListView, self).get_context_data(**kwargs)
print(context)
#context['xxx'] = 'xxx' #添加额外的键值对,传到模板中
return context
{'paginator': <django.core.paginator.Paginator object at 0x0415D310>, 'page_obj': <Page 3 of 10>, 'is_paginated': True, 'object_list': <QuerySet [<Article: Article
object (21)>, <Article: Article object (22)>, <Article: Article object (23)>, <Article: Article object (24)>, <Article: Article object (25)>, <Article: Article obje
ct (26)>, <Article: Article object (27)>, <Article: Article object (28)>, <Article: Article object (29)>, <Article: Article object (30)>]>, 'articles': <QuerySet [<
Article: Article object (21)>, <Article: Article object (22)>, <Article: Article object (23)>, <Article: Article object (24)>, <Article: Article object (25)>, <Arti
cle: Article object (26)>, <Article: Article object (27)>, <Article: Article object (28)>, <Article: Article object (29)>, <Article: Article object (30)>]>, 'view':
<book.views.ArticleListView object at 0x0407BCB0>}
字段说明:
"""
paginator: 分页
page_obj:当前这一页参数
is_paginated: True
object_list: 对象名字
view:视图名字
"""
"""
Paginator常用属性和方法:
count:总共有多少条数据。
num_pages:总共有多少页。
page_range:页面的区间。比如有三页,那么就range(1,4)。
Page常用属性和方法:
has_next:是否还有下一页。
has_previous:是否还有上一页。
next_page_number:下一页的页码。
previous_page_number:上一页的页码。
number:当前页。
start_index:当前这一页的第一条数据的索引值。
end_index:当前这一页的最后一条数据的索引值。
"""
# def get_queryset(self):
#例如用户数据的过滤。例如在生产环境中,对删除帖子而实际上还存在表中的数据过滤掉
# return Article.objects.all() #默认返回全部
# return Article.objects.filter(id__lte=9)
模板中上一页:
<nav aria-label="Page navigation">
<ul>
{% for article in articles %}
<li>
{{article.title}}
</li>
{% endfor %}
</ul>
<ul class="pagination">
<!--#上一页-->
<!--当有上一页时-->
{% if page_obj.has_previous %}
<li><a href="{% url 'book:article_list' %}?page={{ page_obj.previous_page_number }}">上一页</a></li>
{% else %}
<li class="disabled"><a href="#">上一页</a></li>
{% endif %}
<!--中间的页码,如果是当前页,添加active属性-->
{% for page in paginator.page_range %}
{% if page == page_obj.number %}
<li class="active">
<a href="{% url 'book:article_list' %}?page={{ page }}">{{ page }}</a>
</li>
{% else %}
<li><a href="{% url 'book:article_list' %}?page={{ page }}">{{ page }}</a></li>
{% endif %}
{% endfor %}
<!--#下一页-->
{% if page_obj.has_next %}
<li><a href="{% url 'book:article_list' %}?page={{ page_obj.next_page_number }}">下一页</a></li>
{% else %}
<li class="disabled"><a href="x">下一页</a></li>
{% endif %}
</ul>
</nav>
标签:number img led dmi return -o 页码 print turn
原文地址:https://www.cnblogs.com/donghaoblogs/p/10763560.html