Customising tags in Django to filter posts in Post model

拈花ヽ惹草 提交于 2019-12-12 03:59:49

问题


Quick question - I'm not sure what would be the correct way to handle this. Essentially I wish to define a custom tag which handles some logic then returns all posts of model Post which has the is_featured field set to True. I have tried a number of avenues to get this working, but none have worked. My last coherant "guess" was the following:

templatetags/blog_tags.py:

@register.inclusion_tag('blog/post/featured_posts.html')
def show_featured_posts(count=4):
    """Return 4 of the most recent posts (of model: 'Post') that has the variable is_featured set to True."""
    if Post.is_featured:
        featured_posts = Post.published.order_by('-publish')[:count]

return { 'featured_posts': featured_posts}

models.py (useful bits):

class PublishedManager(models.Manager):

    def get_queryset(self):
        return super(PublishedManager, self).get_queryset().filter(status='published')

class Post(models.Model):

    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )

    POST_TYPES = (
        ('news', 'News'),
        ('feature', 'Feature'),
        ('review', 'Review'),
    )

    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date='publish')

    author = models.ForeignKey(UserProfile, related_name='blog_posts')

    body = models.TextField()
    lead_in = models.CharField(max_length=500, default='')

    #These next items shall contain our development information for game reviews - this is much like the lead_in:
    platform = models.CharField(max_length=1000, default='')
    publisher = models.CharField(max_length=1000, default='')
    developer = models.CharField(max_length=1000, default='')
    release = models.DateTimeField(default=timezone.now)
    is_featured = models.BooleanField(default=False)

    #Out blog layout dictates each featurette has up to three scrolling images associated to it:
    image_scroll_1 = models.ImageField(storage=site_media_upload_location, null=True, blank=True)
    image_scroll_2 = models.ImageField(storage=site_media_upload_location, null=True, blank=True)
    image_scroll_3 = models.ImageField(storage=site_media_upload_location, null=True, blank=True)

    type = models.CharField(max_length=10,choices=POST_TYPES,default='review')

    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,choices=STATUS_CHOICES,default='draft')
    rating = models.PositiveSmallIntegerField(default=10)

    wrap_header = models.CharField(max_length=250, default='')
    wrap_up = models.TextField(default='')
    disclaimer = models.TextField(default='')

    objects = models.Manager()
    published = PublishedManager()
    tags = TaggableManager()

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

    def __str__(self):
        """Return the state title of the post"""
        return self.title

    def get_absolute_url(self):
        """Get absolute_url path specific to this post."""
        return reverse('blog:post_detail', args = [self.publish.year, self.publish.strftime('%m'), self.publish.strftime('%d'), self.slug])

    def get_image(self):
        """Get upload_to path specific to this photo."""
        return self.image.url

That is everything I think I need to include - I have the featured_posts.html template all ready to go so I don't think the issue lies with that. It's purely in the blog_tags.py file.


回答1:


replace the following code in blog_tags.py

 if Post.is_featured:
        featured_posts = Post.published.order_by('-publish')[:count]

to

featured_posts = Post.published.filter(is_featured=True).order_by('-publish')[:count]


来源:https://stackoverflow.com/questions/44346129/customising-tags-in-django-to-filter-posts-in-post-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!