获取查询集中的下一个项目

时间:2019-10-19 19:52:48

标签: python django web view voting

以下是“投票”网络项目中的摘录,我正尝试在投票后获得下一个类别...

当用户成功投票后,我希望页面重定向到下一个类别...我要做的是获取该类别的ID,然后将变量保存为next_category,原始类别的ID会增加一个( next_category = category.id +1)。

但是您知道Django并不是严格按照ID顺序排列的,所以如果我之前删除过类别,可以说cat 5和cat1 cat2被认为是cat10,当它到达cat4时,就会出现问题< / p>

“被提名人”视图给出了特定类别的页面,“投票”视图是表单处理视图。 希望它能解释得足够。

@login_required
def Nominees(request, category_id):
    categories = Category.objects.get(id=category_id)
    next_category = categories.id +1
    college_id = categories.College.id
    if lastday < today:
        messages.info(request, "Window closed")
    context = {'categories':categories, 'college_id':college_id, 'lastday':lastday, 'today':today,'next_category':next_category}
    return render(request, 'nomination/contestants.html', context )

def Voting(request):
    if request.method == "POST": 
        vote_user = request.POST.get('vote_user')
        vote_contestant = request.POST.get('vote_contestant')
        vote_category = request.POST.get('vote_category')
        next_category = request.POST.get('next_cat')
        vote_college = request.POST.get('vote_college')
        college = College.objects.get(pk=int(vote_college))
        college_id = college.id
        nex = int(next_category)-1
        try:
            Vote.objects.filter(user_id=request.user.id, category_id=vote_category)[0]
            messages.error(request, "You've already voted")
            return HttpResponseRedirect(reverse('nomination:nominees', args=(vote_category,)))
        except(IndexError, Vote.DoesNotExist):
            if lastday < today:
                messages.error(request, "Too late")
                return HttpResponseRedirect(reverse('nomination:nominees', args=(vote_category,)))
            else:
                new_vote = Vote(contestant_id=vote_contestant, category_id=vote_category, college_id=vote_college, user_id=vote_user)
                new_vote.save()
                messages.success(request, "Successfully voted!!")
                try:
                    categories = college.category_set.filter(id=next_category, College_id=college_id)[nex]
                    category_id=categories.id
                    return HttpResponseRedirect(reverse('nomination:nominees', args=(category_id,)))
                except (IndexError, Category.DoesNotExist):
                    categories = college.category_set.first()
                    category_id = categories.id
                    return HttpResponseRedirect(reverse('nomination:categories', args=(college_id,)))

如果能找到更好的方法,例如使用下一个我尝试过的函数,但仍然给我带来问题,我也将不胜感激。

1 个答案:

答案 0 :(得分:0)

因此,以防万一某些人仍在尝试找出答案,后来我发现了另一种方法,我将queryset转换为列表并使用了index函数:

def Nominees(request, category_id):
    category = Category.objects.get(id=category_id)
    college_id = category.College.id
    college = College.objects.get(id=college_id)
    categories = list(college.category_set.all())
    next_category = (categories.index(category)) + 1
    if lastday < today:
        messages.info(request, "Window closed")
    context = {'categories':category, 'college_id':college_id, 'lastday':lastday, 'today':today,'next_category':next_category}
    return render(request, 'nomination/contestants.html', context )

def Next(request):
    next_category = request.POST.get('next_cat')
    college_id = request.POST.get('college_id')
    college = College.objects.get(id=int(college_id))
    nex = int(next_category)
    nxcate = list(college.category_set.all())
    if request.method =="POST":
        try:
            name = nxcate[nex]
            nxcat = college.category_set.filter(Name=name)[0]
            print(nxcat, nxcat.id)
            return HttpResponseRedirect(reverse('nomination:nominees', args=(name.id,)))
        except IndexError:
            name = nxcate[0]
            print('\n',name)
            messages.info(request,"Thank you for voting!!!")
            return HttpResponseRedirect(reverse('nomination:categories', args=(college_id,)))