使用基于Django类的视图倾斜上一个博客和下一个博客

发布于 2025-01-22 18:30:03 字数 1880 浏览 0 评论 0原文

如果我打开博客详细信息,则应该有上一个博客和下一个博客标题作为分页。从Django文档中,我按页码实现了分页。但是,如何通过上一个博客和下一个博客标题来实现分页。

这是我的model.py:views.py

class Post(models.Model):
    title = models.CharField(max_length=250, unique=True)
    slug = models.SlugField(max_length=250, unique=True, null=True, blank=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="blog_posts")
    cover = models.ImageField(upload_to="Uploaded/")
    content = RichTextUploadingField(blank=True, null=True)
    sticker = models.CharField(max_length=50, unique=False, null=True, blank=True)
    published_at = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)
    status = models.IntegerField(choices=STATUS, default=0)
    tags = models.CharField(max_length=400, null=True, blank=True)

    class Meta:
        ordering = ['-published_at']
    
    def __str__(self):
        return self.title
    

post_detail.html

class PostDetail(generic.DetailView):
    model = Post
    template_name = 'blogs/post_detail.html'

{% extends "blogs/base.html" %}
{% load static %}
{% block post %}
<section class="services">
    <dev class="box-container">
        <div class="box-lg">
            <h3>{{ post.title }}</h3>
            <img src="/Images/{{ post.cover }}"  >
            <hr>
            <small>{{ post.published_at }}</small>
            <p>{{ post.content | safe }}</p>
        </div>
    </dev>
</section>

<!-- Pagination Start -->
<div class="center">
</div>
<!-- Pagination Ends -->
{% endblock post %}

现在,请帮助我如何完成标题为“分页”的Prev-Next博客,如下图

标题为分页图像

If I open a blog detail, there should be the previous blog and next blog title as a pagination. From Django documentation, I implemented the pagination by page number. But how can I implement the pagination by previous blog and next blog title.

Here is my model.py:

class Post(models.Model):
    title = models.CharField(max_length=250, unique=True)
    slug = models.SlugField(max_length=250, unique=True, null=True, blank=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="blog_posts")
    cover = models.ImageField(upload_to="Uploaded/")
    content = RichTextUploadingField(blank=True, null=True)
    sticker = models.CharField(max_length=50, unique=False, null=True, blank=True)
    published_at = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)
    status = models.IntegerField(choices=STATUS, default=0)
    tags = models.CharField(max_length=400, null=True, blank=True)

    class Meta:
        ordering = ['-published_at']
    
    def __str__(self):
        return self.title
    

views.py

class PostDetail(generic.DetailView):
    model = Post
    template_name = 'blogs/post_detail.html'

post_detail.html

{% extends "blogs/base.html" %}
{% load static %}
{% block post %}
<section class="services">
    <dev class="box-container">
        <div class="box-lg">
            <h3>{{ post.title }}</h3>
            <img src="/Images/{{ post.cover }}"  >
            <hr>
            <small>{{ post.published_at }}</small>
            <p>{{ post.content | safe }}</p>
        </div>
    </dev>
</section>

<!-- Pagination Start -->
<div class="center">
</div>
<!-- Pagination Ends -->
{% endblock post %}

Now please help me how to do the prev-next blog titled pagination like the below image!

titled pagination image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身狗的梦 2025-01-29 18:30:03

您可以处理此操作的一种方法是使用.get_previous_by_foo和.get_next_by_foo您可以在此处阅读更多: https://docs.djangoproject.com/en/2.1/ref/ref/ref/mmodels/mmodels/mmodels/instances/instances/#django.django.db.models.model.model.model.model.model.get_model.get_nexnexte

这只是一个想法,您可以做到这一点。

{% extends "blogs/base.html" %}
{% load static %}
{% block post %}
<section class="services">
    <dev class="box-container">
        <div class="box-lg">
            <h3>{{ post.title }}</h3>
            <img src="/Images/{{ post.cover }}"  >
            <hr>
            <small>{{ post.published_at }}</small>
            <p>{{ post.content | safe }}</p>
        </div>
    </dev>
</section>

<!-- Pagination Start -->
<div class="center">
{{ post.get_previous_by_published_at.title }}
</div>
<!-- Pagination Ends -->
<div class="center">
{{ post.get_next_by_published_at.title }}
</div>

{% endblock post %}

正如我所说的那样,这只是现实生活中的一个想法,您必须检查下一个或上一篇文章是否存在或可能是最后一篇文章。

One way you can handle this is to use .get_previous_by_Foo and .get_next_by_Foo you can read more here : https://docs.djangoproject.com/en/2.1/ref/models/instances/#django.db.models.Model.get_next_by_FOO

This is just an idea how you can do it.

{% extends "blogs/base.html" %}
{% load static %}
{% block post %}
<section class="services">
    <dev class="box-container">
        <div class="box-lg">
            <h3>{{ post.title }}</h3>
            <img src="/Images/{{ post.cover }}"  >
            <hr>
            <small>{{ post.published_at }}</small>
            <p>{{ post.content | safe }}</p>
        </div>
    </dev>
</section>

<!-- Pagination Start -->
<div class="center">
{{ post.get_previous_by_published_at.title }}
</div>
<!-- Pagination Ends -->
<div class="center">
{{ post.get_next_by_published_at.title }}
</div>

{% endblock post %}

as i said this just an idea in real life you have to check that next or previous post exists or maybe that post is the last one a thing like that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文