JonathanHayward JonathanHayward - 1 month ago 5
Python Question

Where is this Python syntax error?

I'm coming back to Python and there's at least one point where I'm making a syntax error in a 0.9.x Pinax installation. What I am trying to do here is add an additional layer of filtering (on top of the default optional filtering that provides functionality allowing the user to either see all blog entries, or all of one specific user's blog entries).

In another file, custom.py, I have a function

threshold_check()
intended to filter another way; it takes two arguments, a Django User and one of several types of objects including a blog post, and returns, true or false, whether that item should be included.

The code that I have looks correct to me, but Django is reporting a SyntaxError on the second line of the list comprehension populating
allowed_blogs
in:

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))


What I am doing wrong, and what do I need to do so that the referenced
custom.threshold_check()
is allowed to approve or veto Pinax blog objects being included in the
allowed_blogs
list?

TIA,

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

This isn't valid Python. Perhaps you meant:

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