Django视图HttpResponseRedirect

时间:2018-09-29 19:25:42

标签: django django-templates django-views http-redirect

views.py中有两个功能:

第一个功能

def upload_blob(request, iterator, interview_id, candidate_id, question_id):
    try:
        interview_obj = Interview.objects.get(id=interview_id)
    except ObjectDoesNotExist:
        interview_obj = None
    current_interview = interview_obj
    if request.method == 'POST':
        //do some operation
        iterator = str(int(iterator) + 1)
        return HttpResponseRedirect(reverse('candidate:show_question', kwargs={'iterator': iterator,'interview_id':current_interview.id,'question_id':question_id}))
    else:
        return render(request, 'candidate/record_answer.html')

第二功能

def show_question(request, iterator, interview_id, question_id):
    try:
        interview_obj = Interview.objects.get(id=interview_id)
    except ObjectDoesNotExist:
        interview_obj = None
    current_interview = interview_obj
    current_question_id = InterviewQuestion.objects.filter(interview_id=interview_obj)[int(iterator)].question_id.id
    current_question = Question.objects.get(id=current_question_id)
    **//Here if redirected from first function, I should display show_question page with incremented iterator value, but currently it's going in loop**
        return HttpResponseRedirect(reverse('candidate:show_question', kwargs={'iterator': iterator,'interview_id':current_interview.id,'question_id':question_id}))
    context = {'iterator':iterator, 'current_interview':current_interview,'current_question':current_question,}
    return render(request, 'candidate/show_question.html', context)
  • **操作流程如下:

    • show_question函数在iterator = 0时调用,并呈现show_question.html。用户在此页面上执行一些调用 通过POST请求获得功能upload_blob。
    • upload_blob在检查request.method是POST之后执行一些操作,将迭代器值增加1并重定向到show_question 方法。
    • 这一次,show_question接收到迭代器= 1,并且应该显示带有iterator = 1的show_question页面(不放置重定向) 环)。再次,用户应该对show_question.html进行一些操作,然后 应该通过POST请求调用upload_blob。

我目前正面临循环和show_question **中重复重定向的问题

0 个答案:

没有答案