Django博客如果使用paginator如何显示每个页面的最新帖子

时间:2017-08-08 01:22:57

标签: python html django

我在django博客中显示最新帖子时遇到问题。我使用了分页器,但现在我无法在每一页上发布最新帖子。

views.py

def post_list(request):
    post_list = Post.objects.all()
    page = request.GET.get('page')

    paginator = Paginator(post_list, per_page=3)
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator(paginator.num_pages)
    return render(request, 'blog/post_list.html', {'posts': posts,'page': page})


def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

models.py

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length=200)
    text = models.TextField()
    image = models.FileField(upload_to='images/')
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

    class Meta:
        ordering = ('-published_date',)

forms.py

from django import forms

from .models import Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = (
            'title', 
            'text', 
            'image',
            )

urls.py

urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
    url(r'^post/new/$', views.post_new, name='post_new'),
    url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),
    url(r'^about$', views.about, name='about'),
    url(r'^projects$', views.projects, name='projects'),
]

post_list.html

  <div class="widget">
    <h3>
      Latest Posts
    </h3>
    <ul class="list-group">
      {% for post in posts %}
      <li class="list-group-item"><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title|truncatewords:5 }}</a></li>
      {% endfor %}
    </ul>
  </div>
  <div class="widget">

现在/?page = 1显示3个最新帖子,/?page = 2只显示一个最新帖子(4个可用) 我想在每一页上显示3条最新帖子

2 个答案:

答案 0 :(得分:1)

无论如何我做了它并且有效

views.py

    def post_list(request):
    post_list = Post.objects.all()
    page = request.GET.get('page')
    forms = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:3]
    paginator = Paginator(post_list, per_page=3)
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator(paginator.num_pages)
    return render(request, 'blog/post_list.html', {'posts': posts,'page': page, 'forms': forms,})



def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    form = post
    return render(request, 'blog/post_detail.html', {'post': post, 'form' : form})

post_list.html

  <div class="widget">
    <h3>
      Latest Posts
    </h3>
    <ul class="list-group">
      {% for form in forms %}
      <li class="list-group-item"><a href="{% url 'post_detail' pk=form.pk %}">{{ form.title|truncatewords:5 }}</a></li>
      {% endfor %}
    </ul>
  </div>

答案 1 :(得分:0)

这不是你想要的,但你可以打开孤儿。这将通过将任何页面发送到私有页面来防止任何页面具有少量项目。

https://docs.djangoproject.com/en/1.11/topics/pagination/#optional-arguments