如何避免在 for-in 循环中使用 return 语句?
How to avoid using a return statement in a for-in loop?
我下面的观点是只经历一个迭代周期,然后打破循环。我知道这可能是因为这一行中的 return 语句:return JsonResponse([post.serialize() for post in posts], safe=False)
但我怎样才能避免这种情况?
我尝试将 return 语句更改为 print 语句,但随后出现 ValueError:
The view network.views.followingPosts didn't return an HttpResponse object. It returned None instead.
就在 except
语句之前,我尝试在那里放置一个 continue 但这没有效果。
我不太确定如何解决这个问题。有什么想法吗?
def followingPosts(request, user_username):
try:
user_profile = get_object_or_404(User, username=user_username)
following_users = user_profile.get_following()
for person in following_users:
posts = Post.objects.filter(creator=person)
except Profile.DoesNotExist:
return JsonResponse({"error": "No user found."}, status=404)
if request.method == "GET":
return JsonResponse([post.serialize() for post in posts], safe=False)
else:
return JsonResponse({
"error": "GET request required."
}, status=400)
作为@quamrana,我认为循环实现得很好,除了你只为最后一个循环存储帖子运行。这是更正:
def followingPosts(request, user_username):
try:
user_profile = get_object_or_404(User, username=user_username)
following_users = user_profile.get_following()
posts = list()
for person in following_users:
posts += Post.objects.filter(creator=person)
except Profile.DoesNotExist:
return JsonResponse({"error": "No user found."}, status=404)
if request.method == "GET":
return JsonResponse([post.serialize() for post in posts], safe=False)
else:
return JsonResponse({
"error": "GET request required."
}, status=400)
此外,在try-clause中保持一行是一个好习惯。因此,
def followingPosts(request, user_username):
try:
user_profile = get_object_or_404(User, username=user_username)
except Profile.DoesNotExist:
return JsonResponse({"error": "No user found."}, status=404)
if request.method == "GET":
following_users = user_profile.get_following()
posts = list()
for person in following_users:
posts += Post.objects.filter(creator=person)
return JsonResponse([post.serialize() for post in posts], safe=False)
else:
return JsonResponse({
"error": "GET request required."
}, status=400)
我下面的观点是只经历一个迭代周期,然后打破循环。我知道这可能是因为这一行中的 return 语句:return JsonResponse([post.serialize() for post in posts], safe=False)
但我怎样才能避免这种情况?
我尝试将 return 语句更改为 print 语句,但随后出现 ValueError:
The view network.views.followingPosts didn't return an HttpResponse object. It returned None instead.
就在 except
语句之前,我尝试在那里放置一个 continue 但这没有效果。
我不太确定如何解决这个问题。有什么想法吗?
def followingPosts(request, user_username):
try:
user_profile = get_object_or_404(User, username=user_username)
following_users = user_profile.get_following()
for person in following_users:
posts = Post.objects.filter(creator=person)
except Profile.DoesNotExist:
return JsonResponse({"error": "No user found."}, status=404)
if request.method == "GET":
return JsonResponse([post.serialize() for post in posts], safe=False)
else:
return JsonResponse({
"error": "GET request required."
}, status=400)
作为@quamrana,我认为循环实现得很好,除了你只为最后一个循环存储帖子运行。这是更正:
def followingPosts(request, user_username):
try:
user_profile = get_object_or_404(User, username=user_username)
following_users = user_profile.get_following()
posts = list()
for person in following_users:
posts += Post.objects.filter(creator=person)
except Profile.DoesNotExist:
return JsonResponse({"error": "No user found."}, status=404)
if request.method == "GET":
return JsonResponse([post.serialize() for post in posts], safe=False)
else:
return JsonResponse({
"error": "GET request required."
}, status=400)
此外,在try-clause中保持一行是一个好习惯。因此,
def followingPosts(request, user_username):
try:
user_profile = get_object_or_404(User, username=user_username)
except Profile.DoesNotExist:
return JsonResponse({"error": "No user found."}, status=404)
if request.method == "GET":
following_users = user_profile.get_following()
posts = list()
for person in following_users:
posts += Post.objects.filter(creator=person)
return JsonResponse([post.serialize() for post in posts], safe=False)
else:
return JsonResponse({
"error": "GET request required."
}, status=400)