Saya memiliki masalah dengan menampilkan posting terbaru di blog Django saya. Saya menggunakan paginator tetapi sekarang saya tidak dapat memunculkan posting terbaru setiap halaman.

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})

Model.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',)

Form.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">

Sekarang /?page=1 menampilkan 3 postingan terbaru, /?page=2 hanya menampilkan satu postingan terbaru (dari 4 yang tersedia) Saya ingin menampilkan 3 postingan terbaru setiap halaman

0
Mateusz Slisz 8 Agustus 2017, 04:22

2 jawaban

Jawaban Terbaik

Saya tetap melakukannya dan berhasil

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
Mateusz Slisz 10 Agustus 2017, 23:16

Ini bukan yang Anda cari, tetapi Anda dapat menghidupkan anak yatim. Ini akan mencegah halaman mana pun dari memiliki sejumlah kecil item dengan mengirimkannya ke halaman sebelumnya.

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

0
Jordan 8 Agustus 2017, 01:29