2017-06-03 62 views
0

快速問題 - 我不確定什麼是正確的方式來處理這個問題。基本上我希望定義一個處理某些邏輯的自定義標籤,然後返回所有具有is_featured字段設置爲True的Post Post的所有帖子。我嘗試了很多途徑來完成這項工作,但都沒有奏效。我最後coherant 「猜測」 是以下幾點:定製Django中的標籤以過濾Post中的帖子

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(有用位):

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 

這就是一切,我想我需要包括 - 我有featured_posts.html模板都準備好了,所以我不認爲問題在於此。它純粹在blog_tags.py文件中。

+0

你能告訴模板? – zaidfazil

+0

@FazilZaid您好Fazil - 無需擔心,只是實施了下面的答案,一切都在工作。無論如何,謝謝你的想法。 –

回答

1

替換blog_tags.py

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

下面的代碼

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

完美工作。我之前在視圖中過濾了Post類 - 所以我真的不應該問這個問題!不過,非常感謝您的幫助。 –