【问题标题】:Where is this Python syntax error?这个 Python 语法错误在哪里?
【发布时间】:2016-09-29 22:34:37
【问题描述】:

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

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

我的代码对我来说看起来是正确的,但是 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,

【问题讨论】:

    标签: python django pinax


    【解决方案1】:
    [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)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多