夹层BlogCategory解析类别 [英] Mezzanine BlogCategory parsing categories

查看:132
本文介绍了夹层BlogCategory解析类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个自定义过滤器,该过滤器将blog_post作为参数,并对类别(附加到Blog帖子)进行一些解析.

I want to build a custom filter that takes a blog_post as an argument and does some parsing of the categories (attached to the blog post).

我尝试过这样:

from mezzanine import template
from mezzanine.blog.models import BlogPost, BlogCategory

register = template.Library()

@register.filter(name='has_friends')
def has_friends(blog_post):
  categories = blog_post.categories.all()
  if 'Friends' in categories:
    return False
  else:
    return True

问题在于blog_post.categories.all()返回的内容如下:

The problem is that blog_post.categories.all() returns something like this:

[<BlogCategory: Enemies>, <BlogCategory: Allies>, <BlogCategory: Friends>, <BlogCategory: Family>]

问题:

  1. 我如何获得像这样解析的类别列表['Enemies','Allies','Friends','Family']而不是上面的类别(为了我的 if 声明起作用)?

没有上面问题1的答案,如何使用IF语句在上面显示的BlogCategory列表中进行搜索?

without the answer at above question 1, how can I use IF statement to search in the BlogCategory list shown above?

谢谢

GG

推荐答案

我自己找到了答案,方法是:我使用dir(category)来获取其方法=>在以下标题中找到:title,slug等. 然后我用:

Found the answer myself, this way: I used dir(category) to get its methods => found among: title, slug, etc... then I use:

for category in categories:
    if category.title == 'Friends':
        # do stuff

这篇关于夹层BlogCategory解析类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆