2016-05-31 118 views
0

我回到Python,至少有一点我在0.9.x Pinax安装中发生语法错误。我在这里要做的是添加一个额外的过滤层(在默认的可选过滤之上,提供允许用户查看所有博客条目或所有特定用户的博客条目的功能)。Python语法错误在哪里?

在另一个文件custom.py中,我有一个函数threshold_check()用于过滤另一种方式;它需要两个参数,一个Django用户和几个类型的对象之一,包括博客文章,并返回true或false,是否应该包含该项目。

,我有代码看起来正确的给我,但Django是报告的列表中理解的填充allowed_blogs的第二行语法错误:

def blogs(request, username=None, template_name="blog/blogs.html"): 
    blogs = Post.objects.filter(status=2).select_related(depth=1).order_by("-publish") 
    if username is not None: 
     user = get_object_or_404(User, username=username.lower()) 
     blogs = blogs.filter(author=user) 
    allowed_blogs = [blog in blogs.objects.all() if 
     custom.threshold_check(request.user, blog)] 
    return render_to_response(template_name, { 
     "blogs": allowed_blogs, 
    }, context_instance=RequestContext(request))

我在做什么错了,我需要什么这样做是否允许引用custom.threshold_check()批准或否决包含在allowed_blogs列表中的Pinax博客对象?

TIA,

回答

4
[blog in blogs.objects.all() if 
    custom.threshold_check(request.user, blog)] 

这不是有效的Python。也许你的意思是:

[blog for blog in blogs.objects.all() if 
    custom.threshold_check(request.user, blog)] 
+0

谢谢;你正确地确定了我想要的东西。 – JonathanHayward