【发布时间】:2017-11-04 21:07:46
【问题描述】:
快速提问 - 我不确定处理此问题的正确方法是什么。本质上,我希望定义一个自定义标签来处理一些逻辑,然后返回模型 Post 的所有帖子,其中 is_featured 字段设置为 True。我已经尝试了多种途径来使其正常工作,但都没有奏效。我最后一个连贯的“猜测”如下:
模板标签/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 文件中。
【问题讨论】:
-
可以展示一下模板吗?
-
@FazilZaid 嗨 Fazil - 不用担心,只需执行下面的答案,现在一切正常。无论如何,谢谢你的想法。
标签: python django django-templates django-taggit