如何解决 python 中的分页器问题
How can I fix this paginator problem in python
在我看来,我将分页功能定义为每页仅显示 2 post,但问题是每次我添加新的 post 时,底部的分页都会发生变化,但是它仍然显示所有 post 而不是两个,我将分享我的代码以便您检查我是否做错了。
views.py
def StockView(request, sym, ):
stock_posts = Post.objects.filter(stock__symbol=sym)
stock_sym = get_object_or_404(StockNames,symbol = sym)
post_list = Post.objects.filter(stock__symbol=sym)
paginator = Paginator(post_list, 2)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'app1/stockview.html', {'page':page,'posts':posts,'stocks':stock_posts, 'stock_sym':stock_sym, 'sym':sym })
models.py
class StockNames(models.Model):
name = models.CharField(max_length=255)
symbol = models.CharField(max_length=255)
def __str__(self):
return self.symbol
class Post(models.Model):
title = models.CharField(max_length= 255)
header_image = models.ImageField(null = True, blank = True, upload_to = 'images/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank = True, null = True)
#body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='coding')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name = 'blog_posts')
stock = models.ForeignKey(StockNames, null=True, on_delete = models.CASCADE)
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
urls.py
app_name = 'app1'
urlpatterns = [
path('add_post/<str:sym>',AddPostView.as_view(), name='addpost'),
path('stock/<str:sym>/', views.StockView, name = 'stock'),
]
pagination.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<nav aria-label="Page navigation example">
<ul class="pagination" >
<li class="page-item" >
{% if page.has_previous %}
<a class="page-link" href="?page={{page.previous_page_number}}">Previous</a>
{% endif %}
</li>
<li class="page-item">
<a class="page-link" href="">Page {{page.number}} of {{page.paginator.num_pages}}</a>
</li>
<li class="page-item" >
{% if page.has_next %}
<a class="page-link" href="?page={{page.next_page_number}}">Next</a>
{% endif %}
</ul>
</nav>
</body>
</html>
模板是我继承了分页
{% extends "app1/base.html" %}
{% block body_block %}
{% if stock_sym %}
<h1> {{sym}} </h1>
<a href ="{% url 'app1:addpost' sym %}">Add Post<span class="sr-only">(current)</span></a>
{% if stocks %}
<ul>
{% for post in stocks %}
<li><a href="{% url 'app1:article-detail' post.pk %}">{{sym}}</a> -
{{post.author}} - <small>{{post.post_date}}</small> -
{% if user.is_authenticated %}
<small><a href="{% url 'app1:updatepost' post.pk %}">Edit</a></small><small>
<a href="{% url 'app1:deletepost' post.pk %}">- Delete</a>
</small></li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<h1>No posts yet</h1>
{% endif %}
{% else %}
<h1>hey</h1>
{% endif %}
{% include 'app1/pagination.html' with page=posts %}
{% endblock %}
这是我的问题图片,所以你可以看到我在说什么:
这里我们添加 stock_posts = paginator.get_page(page)
以按页面显示数据。
请试试这个:
def StockView(request, sym, ):
stock_posts = Post.objects.filter(stock__symbol=sym)
stock_sym = get_object_or_404(StockNames,symbol = sym)
post_list = Post.objects.filter(stock__symbol=sym)
paginator = Paginator(post_list, 2)
page = request.GET.get('page')
stock_posts = paginator.get_page(page)
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'app1/stockview.html', {'page':page,'posts':posts,'stocks':stock_posts, 'stock_sym':stock_sym, 'sym':sym })
在我看来,我将分页功能定义为每页仅显示 2 post,但问题是每次我添加新的 post 时,底部的分页都会发生变化,但是它仍然显示所有 post 而不是两个,我将分享我的代码以便您检查我是否做错了。
views.py
def StockView(request, sym, ):
stock_posts = Post.objects.filter(stock__symbol=sym)
stock_sym = get_object_or_404(StockNames,symbol = sym)
post_list = Post.objects.filter(stock__symbol=sym)
paginator = Paginator(post_list, 2)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'app1/stockview.html', {'page':page,'posts':posts,'stocks':stock_posts, 'stock_sym':stock_sym, 'sym':sym })
models.py
class StockNames(models.Model):
name = models.CharField(max_length=255)
symbol = models.CharField(max_length=255)
def __str__(self):
return self.symbol
class Post(models.Model):
title = models.CharField(max_length= 255)
header_image = models.ImageField(null = True, blank = True, upload_to = 'images/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank = True, null = True)
#body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='coding')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name = 'blog_posts')
stock = models.ForeignKey(StockNames, null=True, on_delete = models.CASCADE)
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
urls.py
app_name = 'app1'
urlpatterns = [
path('add_post/<str:sym>',AddPostView.as_view(), name='addpost'),
path('stock/<str:sym>/', views.StockView, name = 'stock'),
]
pagination.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<nav aria-label="Page navigation example">
<ul class="pagination" >
<li class="page-item" >
{% if page.has_previous %}
<a class="page-link" href="?page={{page.previous_page_number}}">Previous</a>
{% endif %}
</li>
<li class="page-item">
<a class="page-link" href="">Page {{page.number}} of {{page.paginator.num_pages}}</a>
</li>
<li class="page-item" >
{% if page.has_next %}
<a class="page-link" href="?page={{page.next_page_number}}">Next</a>
{% endif %}
</ul>
</nav>
</body>
</html>
模板是我继承了分页
{% extends "app1/base.html" %}
{% block body_block %}
{% if stock_sym %}
<h1> {{sym}} </h1>
<a href ="{% url 'app1:addpost' sym %}">Add Post<span class="sr-only">(current)</span></a>
{% if stocks %}
<ul>
{% for post in stocks %}
<li><a href="{% url 'app1:article-detail' post.pk %}">{{sym}}</a> -
{{post.author}} - <small>{{post.post_date}}</small> -
{% if user.is_authenticated %}
<small><a href="{% url 'app1:updatepost' post.pk %}">Edit</a></small><small>
<a href="{% url 'app1:deletepost' post.pk %}">- Delete</a>
</small></li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<h1>No posts yet</h1>
{% endif %}
{% else %}
<h1>hey</h1>
{% endif %}
{% include 'app1/pagination.html' with page=posts %}
{% endblock %}
这是我的问题图片,所以你可以看到我在说什么:
这里我们添加 stock_posts = paginator.get_page(page)
以按页面显示数据。
请试试这个:
def StockView(request, sym, ):
stock_posts = Post.objects.filter(stock__symbol=sym)
stock_sym = get_object_or_404(StockNames,symbol = sym)
post_list = Post.objects.filter(stock__symbol=sym)
paginator = Paginator(post_list, 2)
page = request.GET.get('page')
stock_posts = paginator.get_page(page)
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'app1/stockview.html', {'page':page,'posts':posts,'stocks':stock_posts, 'stock_sym':stock_sym, 'sym':sym })