当前位置:优学网  >  在线题库

如何在django中获得上一个和下一个相关职位?

发表时间:2022-07-30 00:46:51 阅读:188

views.py

def post_details(request,pk):
    post = Post.objects.get(id=pk)
    # next_post = Post.objects.filter(id=pk)
    context={'post':post,'next':next_post}
    return render(request, 'blog/post_detail.html', context)

blog-detail

<div class="s-content__pagenav group">
    <div class="prev-nav">
        <a href="#" rel="prev">
            <span>Previous</span>
            Tips on Minimalist Design 
        </a>
    </div>

     <div class="next-nav">
         <a href="#" rel="next">
             <span>Next</span>
            Less Is More 
         </a>
     </div>

</div> 

型号 _#这是我的模型类用户(AbstractUser):

通过

    name = models.CharField(max_length=200)
    bio = models.TextField(null=True)
    email = models.EmailField(unique=True, null=True)
    avatar = models.ImageField( null=True, upload_to='blog_media', default="images/avatar.svg")
    facebook = models.URLField(blank=True, null=True)
    twitter = models.URLField(blank=True, null=True)
    dribbble = models.URLField(blank=True, null=True)
    instagram = models.URLField(blank=True, null=True)

class Category(models.Model):
    name = models.CharField(max_length=20)

    class Meta:
        verbose_name = 'Category'
        verbose_name_plural = 'Categories'
    def __str__(self):
        return self.name

    class Post(models.Model):
        author = models.ForeignKey(User, on_delete=models.CASCADE)
        category = models.ManyToManyField(Category)
        title = models.CharField(max_length=200, blank=False);
        description = models.TextField(null=True,blank=True)
        image = models.ImageField(upload_to='blog_media') 
        url = models.URLField(null=True, blank=True)
        body = HTMLField()
        created = models.DateTimeField(auto_now=True)
        updated = models.DateTimeField(auto_now_add=True)
        def __str__(self):
            return self.title
🎖️ 优质答案
相关问题