@increment_visitor_counter
def survey(request, survey_code, code, page=1):
    answer = get_object_or_404(
        SurveyAnswer.objects.select_related('survey'),
        survey__is_active=True,
        survey__code=survey_code,
        code=code)

    answer.update_status(answer.STARTED)

    pages = answer.survey.pages()

    # Only look at valid pages
    try:
        page = int(page)
        groups = pages[page - 1]
    except (IndexError, __HOLE__, ValueError):
        return redirect(answer)

    kwargs = {
        'questions': Question.objects.filter(group__in=groups).order_by(
            'group', 'ordering').select_related('group'),
        'answer': answer,
    }

    if request.method == 'POST':
        form = QuestionForm(request.POST, **kwargs)

        if form.is_valid():
            form.save()

            if 'finish' in request.POST:
                answer.update_status(answer.FINISHED)

                return redirect(
                    'survey_survey_end',
                    survey_code=survey_code,
                    code=code)
            elif 'prev' in request.POST:
                offset = -1
            else:
                offset = 1

            if 0 < page + offset <= len(pages):
                return redirect(
                    'survey_survey_page',
                    survey_code=survey_code,
                    code=code,
                    page=page + offset)

    else:
        form = QuestionForm(**kwargs)

    return render(request, 'survey/form.html', {
        'survey': answer.survey,
        'answer': answer,
        'form': form,

        'page': page,
        'page_count': len(pages),
        'is_first_page': page == 1,
        'is_last_page': page == len(pages),
    })
dataset/ETHPy150Open matthiask/survey/survey/views.py/survey