Django -从一个类别的帖子移动到下一个类别的帖子

cpjpxq1n  于 2023-03-13  发布在  Go
关注(0)|答案(1)|浏览(115)

有一个模型和职位附加到它。我不能弄清楚如何使从活动职位的过渡到下一个和回来。我读了很多文章,但我没有找到适合我的。这是我的代码。这怎么能做到呢?
超文本标记语言

<div class="bootons-navigation">
<span class="span__bootons-navigatiom"><a href="#" title="previous">previous</a></span>
<span class="span__bootons-navigatiom"><a href="#" title="next">next</a></span>
</div>

型号

class home(models.Model):
   slug = models.SlugField(max_length=266, unique=True, db_index=True, verbose_name=('URL'))
   title = models.CharField(max_length=250)
   top_description = models.TextField(max_length=700, blank=False)

class Meta:
    verbose_name = 'Главная'
    verbose_name_plural = 'Главная'

def __str__(self):
    return self.title

class category(models.Model):
   slug = models.SlugField(max_length=266, unique=True, db_index=True, verbose_name=('URL'))
   description = models.TextField(max_length=700, blank=False)

   def __str__(self):
    return self.slug

   def get_absolute_url(self):
    return reverse('category', kwargs={'category_slug': self.slug})

class post(models.Model):
   slug = models.SlugField(max_length=266, unique=True, db_index=True, verbose_name=('URL'))
   description = models.TextField(max_length=700, blank=False)

   cat = models.ForeignKey('category', on_delete=models.PROTECT, null=True)
    
   def __str__(self):
    return self.slug

   def get_absolute_url(self):
    return reverse('post', kwargs={'post_slug': self.slug})

意见

def content_category(request, category_slug):
  categorypost=category.objects.filter( slug=category_slug)
  get_list_or_404(categorypost, slug=category_slug)
  return render(request, 'content/season.html',{
  'category':categorypost,
  })


def content_post(request, category_slug, post_slug):
  postpages = post.objects.filter(slug=post_slug, cat__slug=category_slug)
  get_object_or_404(postpages, slug=post_slug, cat__slug=category_slug)
  return render(request, 'content/post.html', {
  'post': postpages,
  })

网址

path('', views.ContentHome.as_view(), name='homepage'),
path('\<slug:category_slug\>', views.content_category),
path('\<slug:category_slug\>/\<slug:post_slug\>', views.content_post)`
vq8itlhq

vq8itlhq1#

首先,为帖子详细信息url添加一个名称。

path('', views.ContentHome.as_view(), name='homepage'),
path('\<slug:category_slug\>', views.content_category),
path('\<slug:category_slug\>/\<slug:post_slug\>', views.content_post, name='post-detail')`

当呈现帖子页面时,期望上一个和下一个帖子在上下文中传递。
您可以在上下文中从它们的示例构建它们的url

<div class="bootons-navigation">
{% if previous_post %}
<span class="span__bootons-navigatiom"><a href="{% url "post-detail" previous_post.category.slug, previous_post.slug %}" title="previous">previous</a></span>
{% endif %}
{% if next_post %}
<span class="span__bootons-navigatiom"><a href="{% url "post-detail" next_post.category.slug, next_post.slug %}" title="next">next</a></span>
{% endif %}
</div>

获取要包含在上下文中的上一篇和下一篇帖子

def content_post(request, category_slug, post_slug):
  postpages = post.objects.filter(slug=post_slug, cat__slug=category_slug)
  current_post = get_object_or_404(postpages, slug=post_slug, cat__slug=category_slug)
  previous_post = post.objects.filter(cat__slug=category_slug, pk__lt=current_post.pk).first()
  next_post = post.objects.filter(cat__slug=category_slug, pk__gt=current_post.pk).first()
  
  return render(request, 'content/post.html', {
      'post': postpages,
      'previous_post': previous_post,
      'next_post': next_post,
  })

相关问题