Django view HttpResponseRedirect - PullRequest
       21

Django view HttpResponseRedirect

0 голосов
/ 29 сентября 2018

У меня есть две функции в 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.Пользователь выполняет некоторую операцию на этой странице, которая вызывает функцию upload_blob через запрос POST.
    • upload_blob выполняет некоторую операцию после проверки request.method - POST, увеличивает значение итератора на 1 и перенаправляет на метод show_question.
    • В этот раз show_question получает итератор = 1 и должен показывать страницу show_question с итератором = 1 (без включения перенаправлений в цикл).Снова пользователь должен выполнить некоторую операцию с show_question.html, а upload_blob должен быть вызван через запрос POST.

В настоящее время я сталкиваюсь с проблемой цикла и повторных перенаправлений в show_question **

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...