2016-05-31 112 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