我回到Python并且至少有一点我在0.9.x Pinax安装中出现语法错误。我在这里尝试做的是添加一个额外的过滤层(在默认的可选过滤之上,提供允许用户查看所有博客条目或所有特定用户的博客条目的功能)。
在另一个文件custom.py中,我有一个函数threshold_check()
,旨在过滤另一种方式;它需要两个参数,Django用户和几种类型的对象之一,包括博客文章,并返回true,false,是否应该包含该项。
我看到的代码对我来说是正确的,但是Django在填充allowed_blogs
的列表理解的第二行报告了一个SyntaxError:
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,
答案 0 :(得分: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)]