在 Mezzanine 中过滤已发布的 BlogPosts
Filter published BlogPosts in Mezzanine
目前在 Mezzanine,我有一些博客 post 准备发布,但日期是几天后。
目前这些博客 post 的对象可以通过上下文返回这些对象来查看。我如何过滤掉这些尚未发布的?如果我去博客 post URL 它给我一个 404 错误,所以这是正确的。
型号:
class BlogPost(Displayable, Ownable, RichText, AdminThumbMixin):
"""
A blog post.
"""
categories = models.ManyToManyField("BlogCategory",
verbose_name=_("Categories"),
blank=True, related_name="blogposts")
allow_comments = models.BooleanField(verbose_name=_("Allow comments"),
default=True)
comments = CommentsField(verbose_name=_("Comments"))
rating = RatingField(verbose_name=_("Rating"))
featured_image = FileField(verbose_name=_("Featured Image"),
upload_to=upload_to("blog.BlogPost.featured_image", "blog"),
format="Image", max_length=255, null=True, blank=True)
related_posts = models.ManyToManyField("self",
verbose_name=_("Related posts"), blank=True)
admin_thumb_field = "featured_image"
class Meta:
verbose_name = _("Blog post")
verbose_name_plural = _("Blog posts")
ordering = ("-publish_date",)
def get_absolute_url(self):
"""
URLs for blog posts can either be just their slug, or prefixed
with a portion of the post's publish date, controlled by the
setting ``BLOG_URLS_DATE_FORMAT``, which can contain the value
``year``, ``month``, or ``day``. Each of these maps to the name
of the corresponding urlpattern, and if defined, we loop through
each of these and build up the kwargs for the correct urlpattern.
The order which we loop through them is important, since the
order goes from least granular (just year) to most granular
(year/month/day).
"""
url_name = "blog_post_detail"
kwargs = {"slug": self.slug}
date_parts = ("year", "month", "day")
if settings.BLOG_URLS_DATE_FORMAT in date_parts:
url_name = "blog_post_detail_%s" % settings.BLOG_URLS_DATE_FORMAT
for date_part in date_parts:
date_value = str(getattr(self.publish_date, date_part))
if len(date_value) == 1:
date_value = "0%s" % date_value
kwargs[date_part] = date_value
if date_part == settings.BLOG_URLS_DATE_FORMAT:
break
return reverse(url_name, kwargs=kwargs)
有没有办法只检索post真正发表的文章?
Is there a way by retrieving ONLY posts that are truly published?
这会使用查询 publish_date <= today
:
获取今天或之前发布的博文
import datetime
BlogPost.objects.filter(publish_date__lte=datetime.date.today())
您看到的是未发布的帖子,因为您已通过工作人员身份验证 - 如果您注销,您将只能看到已发布的帖子。
目前在 Mezzanine,我有一些博客 post 准备发布,但日期是几天后。
目前这些博客 post 的对象可以通过上下文返回这些对象来查看。我如何过滤掉这些尚未发布的?如果我去博客 post URL 它给我一个 404 错误,所以这是正确的。
型号:
class BlogPost(Displayable, Ownable, RichText, AdminThumbMixin):
"""
A blog post.
"""
categories = models.ManyToManyField("BlogCategory",
verbose_name=_("Categories"),
blank=True, related_name="blogposts")
allow_comments = models.BooleanField(verbose_name=_("Allow comments"),
default=True)
comments = CommentsField(verbose_name=_("Comments"))
rating = RatingField(verbose_name=_("Rating"))
featured_image = FileField(verbose_name=_("Featured Image"),
upload_to=upload_to("blog.BlogPost.featured_image", "blog"),
format="Image", max_length=255, null=True, blank=True)
related_posts = models.ManyToManyField("self",
verbose_name=_("Related posts"), blank=True)
admin_thumb_field = "featured_image"
class Meta:
verbose_name = _("Blog post")
verbose_name_plural = _("Blog posts")
ordering = ("-publish_date",)
def get_absolute_url(self):
"""
URLs for blog posts can either be just their slug, or prefixed
with a portion of the post's publish date, controlled by the
setting ``BLOG_URLS_DATE_FORMAT``, which can contain the value
``year``, ``month``, or ``day``. Each of these maps to the name
of the corresponding urlpattern, and if defined, we loop through
each of these and build up the kwargs for the correct urlpattern.
The order which we loop through them is important, since the
order goes from least granular (just year) to most granular
(year/month/day).
"""
url_name = "blog_post_detail"
kwargs = {"slug": self.slug}
date_parts = ("year", "month", "day")
if settings.BLOG_URLS_DATE_FORMAT in date_parts:
url_name = "blog_post_detail_%s" % settings.BLOG_URLS_DATE_FORMAT
for date_part in date_parts:
date_value = str(getattr(self.publish_date, date_part))
if len(date_value) == 1:
date_value = "0%s" % date_value
kwargs[date_part] = date_value
if date_part == settings.BLOG_URLS_DATE_FORMAT:
break
return reverse(url_name, kwargs=kwargs)
有没有办法只检索post真正发表的文章?
Is there a way by retrieving ONLY posts that are truly published?
这会使用查询 publish_date <= today
:
import datetime
BlogPost.objects.filter(publish_date__lte=datetime.date.today())
您看到的是未发布的帖子,因为您已通过工作人员身份验证 - 如果您注销,您将只能看到已发布的帖子。