嘿伙计们,我的代码有点混乱,但也许你可以帮我弄清楚为什么它没有做我想做的事情。
我的逻辑有问题,因为我自己尝试了更新语句并且它们确实有效。
我不知道为什么它不会进入 elif 语句。
投票表只是 postID、用户名、upvote 和 downvote。如果用户在表中有该 postID 的记录并且他们的赞成票标记为 1,我希望它能够正常工作,那么您应该只能反对票。
目前,它不允许您投票两次,但如果您已经投票,它根本不允许您投票。
这是我的相关代码。
谢谢!
def upvote(request):
postID = request.POST.get("postID")
currentUser = request.POST.get("currentUser")
username = request.POST.get("username")
query = NewPost.objects.get(id = postID)
check = Vote.objects.filter(postID = postID) & Vote.objects.filter(username = currentUser)
if(len(check) < 1):
query.rating = query.rating +1
query.save()
query2 = User.objects.get(username = username)
query2.userRanking = query2.userRanking +1
query2.save()
new = Vote.objects.create(postID = postID, username = currentUser, downvote = 0, upvote = 1)
new.save()
pyautogui.hotkey('f5')
return render(request)
elif(len(check) > 1):
if(check[0].upvote == 0):
check.update(downvote = 0)
check.update(upvote = 1)
check[0].save()
check.save()
query.rating = query.rating +1
query.save()
query2 = User.objects.get(username = username)
query2.userRanking = query2.userRanking +1
query2.save()
pyautogui.hotkey('f5')
return render(request)
else:
pyautogui.hotkey('f5')
return render(request)
else:
pyautogui.hotkey('f5')
return render(request)
def downvote(request):
postID = request.POST.get("postID")
currentUser = request.POST.get("currentUser")
username = request.POST.get("username")
query = NewPost.objects.get(id = postID)
check = Vote.objects.filter(postID = postID) & Vote.objects.filter(username = currentUser)
if(len(check) < 1):
query.rating = query.rating -1
query.save()
query2 = User.objects.get(username = username)
query2.userRanking = query2.userRanking -1
query2.save()
new = Vote.objects.create(postID = postID, username = currentUser, downvote = 1, upvote = 0)
new.save()
pyautogui.hotkey('f5')
return render(request)
elif(len(check) > 1):
if(check[0].downvote == 0):
check.update(downvote = 1)
check.update(upvote = 0)
check[0].save()
query.rating = query.rating -1
query.save()
query2 = User.objects.get(username = username)
query2.userRanking = query2.userRanking -1
query2.save()
pyautogui.hotkey('f5')
return render(request)
else:
pyautogui.hotkey('f5')
return render(request)
else:
pyautogui.hotkey('f5')
return render(request)