【发布时间】:2021-04-25 11:01:39
【问题描述】:
我有分页,html:
{% extends 'base.html' %}
{% load pagination_tags %}
{% block title %}NewsLine{% endblock title %}
{% load humanize %}
{% block content %}
{% autopaginate news news_on_page %}
{% paginate %}
News on page:
<input type="submit" value="10" />
<input type="submit" value="20" />
<input type="submit" value="50" />
<div class="container mt-3">
<div class="row my-5">
<div class="col-11">
<p>News overall {{ paginator.count }}</p>
<p>Number of pages {{ paginator.num_pages }}</p>
<p>Page range {{ paginator.page_range }}</p>
{% for peace_of_news in news %}
<div class="p-3">
<h2>{{ peace_of_news.title }}</h2>
<p><small>{{ peace_of_news.date|naturaltime }}</small></p>
<p>{{ peace_of_news.text }}</p>
</div>
{% endfor %}
</div>
</div>
</div>
{% endblock content %}
views.py:
from django.shortcuts import render
from . models import PieceOfNews
from django.core.paginator import Paginator
from django import forms
def index(request):
news = PieceOfNews.objects.all().order_by('-date')
paginator = Paginator(news, 10)
all = list(PieceOfNews.objects.all())
news_on_page = 3
context = {'news': news, 'paginator': paginator, 'all': all, 'news_on_page': news_on_page}
class Meta:
verbose_name_plural = 'piece of news'
return render(request, 'index.html', context)
def get_value():
news_on_page = forms.IntegerField()
如何通过按钮获得一些价值来调整页面上的新闻数量 例如,用户有选择:每页 10 条新闻、每页 20 条新闻和每页 50 条新闻。 如何在 html 表单或视图中发送此值以在模板中再次返回它的值? 要么 也许有更好的方法 做分页哪些用户可以调整?
【问题讨论】:
-
你能分享渲染这个模板的视图文件吗?
-
是的,我编辑了它。谢谢
标签: python django templates view pagination