in my views I defined the paginator function to show only 2 posts per page, but the thing is that every time I add a new post the pagination in the bottom changes, but it still shows all the posts instead of two, I'm going to share my code so you can check if I'm doing any mistakes.
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>
template were I inherit the pagination
{% 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 %}
Here's my problem in pictures so you can see what I'm talking about:
Here we add stock_posts = paginator.get_page(page)
to show data as page wise.
Please Try this:
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 })