Django Pagination не отображается
пытаюсь заставить Pagination работать в моем блоге django.
У меня работает пагинация в файле home.html, но когда я хочу использовать ее в файле categories.html, код не работает.
Есть идеи, почему это работает на home.html, но не работает на categories.html?
Я думаю, что это единственные файлы, которые вам нужно посмотреть, если я ошибаюсь, спросите меня, и я размещу больше. Буду очень рад помощи в этом вопросе.
Расположение программы views.py
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post, Category, Comment
from .forms import PostForm, EditForm, CommentForm
from django.urls import reverse_lazy, reverse
from django.http import HttpResponseRedirect
def LikeView(request, pk):
post = get_object_or_404(Post, id=request.POST.get('post_id'))
liked = False
if post.likes.filter(id=request.user.id).exists():
post.likes.remove(request.user)
liked = False
else:
post.likes.add(request.user)
liked = True
return HttpResponseRedirect(reverse('article-detail', args=[str(pk)]))
class HomeView(ListView):
model = Post
template_name = 'home.html'
cats = Category.objects.all()
ordering = ['-post_date']
paginate_by = 5
def get_context_data(self, *args, **kwargs):
cat_menu = Category.objects.all()
context = super(HomeView, self).get_context_data(*args, **kwargs)
context["cat_menu"] = cat_menu
return context
def CategoryListView(request):
cat_menu_list = Category.objects.all()
return render(request, 'category_list.html', {'cat_menu_list':cat_menu_list})
def CategoryView(request, cats):
category_posts = Post.objects.filter(category=cats.replace('-', ' '))
return render(request, 'categories.html', {'cats':cats.replace('-', ' ').title(), 'category_posts':category_posts})
class ArticleDetailView(DetailView):
model = Post
template_name = 'article_details.html'
def get_context_data(self, *args, **kwargs):
cat_menu = Category.objects.all()
context = super(ArticleDetailView, self).get_context_data(*args, **kwargs)
stuff = get_object_or_404(Post, id=self.kwargs['pk'])
total_likes = stuff.total_likes()
liked = False
if stuff.likes.filter(id=self.request.user.id).exists():
liked = True
context["cat_menu"] = cat_menu
context["total_likes"] = total_likes
context["liked"] = liked
return context
class AddPostView(CreateView):
model = Post
form_class = PostForm
template_name = 'add_post.html'
class AddCommentView(CreateView):
model = Comment
form_class = CommentForm
template_name = 'add_comment.html'
def form_valid(self, form):
form.instance.post_id = self.kwargs['pk']
return super().form_valid(form)
success_url = reverse_lazy('home')
class AddCategoryView(CreateView):
model = Category
template_name = 'add_category.html'
fields = '__all__'
class UpdatePostView(UpdateView):
model = Post
form_class = EditForm
template_name = 'update_post.html'
class DeletePostView(DeleteView):
model = Post
template_name = 'delete_post.html'
success_url = reverse_lazy('home')
шаблоны home.html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<h1 class="headerh1">News Blog</h1>
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-8 mt-3 left">
{% for post in object_list %}
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title"><a href="{% url 'article-detail' post.pk %}">{{post.title }}</a></h2>
<p class="card-text text-muted h6">{{ post.author.first_name }} {{post.author.last_name }} | {{ post.post_date }}
<a href="{% url 'category' post.category|slugify %}">{{ post.category }}</a></p>
<p class="card-text">{{ post.body|safe|slice:":200" }}</p>
<a href="{% url 'article-detail' post.pk %}" class="btn btn-info">Read More</a>
{% if user.is_authenticated %}
{% if user.id == post.author.id %}
<a href="{% url 'update_post' post.pk %}" class="btn btn-primary">Edit</a>
<a href="{% url 'delete_post' post.pk %}" class="btn btn-danger">Delete</a>
{% endif %}
{% endif %}
</div>
</div>
{% endfor %}
</div>
</div>
{% if is_paginated %}
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}" class="page-link">« PREV </a></li>
{% endif %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}" class="page-link"> NEXT »</a></li>
{% endif %}
</ul>
</nav>
{% endif %}
</div>
{% endblock %}
шаблоны categories.html
{% extends 'base.html' %}
{% block content %}
{% if category_posts %}
<h1 class="headerh1">{{ cats }}</h1>
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-8 mt-3 left">
{% for post in category_posts %}
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title"><a href="{% url 'article-detail' post.pk %}">{{post.title }}</a></h2>
<p class="card-text text-muted h6">{{ post.author.first_name }} {{post.author.last_name }} | {{ post.post_date }}
<a href="{% url 'category' post.category|slugify %}">{{ post.category }}</a></p>
<p class="card-text">{{ post.body|safe|slice:":200" }}</p>
<a href="{% url 'article-detail' post.pk %}" class="btn btn-info">Read More</a>
{% if user.is_authenticated %}
{% if user.id == post.author.id %}
<a href="{% url 'update_post' post.pk %}" class="btn btn-primary">Edit</a>
<a href="{% url 'delete_post' post.pk %}" class="btn btn-danger">Delete</a>
{% endif %}
{% endif %}
</div>
</div>
{% endfor %}
</div>
</div>
{% if is_paginated %}
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}" class="page-link">« PREV </a></li>
{% endif %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}" class="page-link"> NEXT »</a></li>
{% endif %}
</ul>
</nav>
{% endif %}
</div>
{% else %}
You got no access here!
{% endif %}