如何使用会话以表格分页形式存储表单数据?

时间:2016-07-20 10:38:25

标签: django session pagination django-sessions

我正在进行一项测验。测验有多个问题 - 表格。由于可能有很多问题,我已经为这些表格创建了一个分页。一切似乎都正常工作,但只有最后一页表格中的数据被发送到服务器。

如何保存以前页面中的数据,以便将所有表格发送到服务器?

我正在考虑使用会话,但无法弄清楚如何使其工作。如您所见,所有问题都采用方法发布的一种形式。但是分页是GET方法。

def take_quiz(request,id):

    if request.method == 'POST':
        quiz = get_object_or_404(models.LanguageQuiz, pk=id)
        sitting = get_object_or_404(models.Sitting, quiz=quiz, user=request.user)
        for k,v in request.POST.iteritems():
            if 'choice-for' in k:
                q = Question.objects.get(pk=k.split('-')[-1])
                choice = Choice.objects.get(pk=v)
                sitting_question = get_object_or_404(SittingQuestion,sitting=sitting,question=q)
                sitting_question.answer = choice
                sitting_question.save()
        correct_answers = len([x for x in sitting.sitting_questions.all() if x.answer.correct])
        result = float(correct_answers)/sitting.sitting_questions.all().count() * 100

        return render(request,'quiz/result.html',context={'result':result,
                                                          'level':level,
                                                          'quiz':quiz})

    if request.method == 'GET':

        with transaction.atomic():
            quiz = get_object_or_404(models.LanguageQuiz, pk=id)

            if models.Sitting.objects.filter(quiz=quiz, user=request.user).exists():
                sitting = get_object_or_404(models.Sitting, quiz=quiz, user=request.user)
                check_expired_sitting(sitting)
                sitting.delete()

            sitting = models.Sitting.objects.create(quiz=quiz, user=request.user)
            sitting.load_questions()

        questions = [x.question for x in sitting.sitting_questions.all()]
        paginator = Paginator(questions,2)
        page_num = request.GET.get('page',1)
        page = paginator.page(page_num)

        context = {'page':page,
                   'quiz':quiz}

        return render(request,'quiz/quiz.html',context=context)

TEMPLATE

{% extends 'base.html' %}
{% block content %}
    <h1>{{ quiz.name }}</h1>
    <br><br><br>
    <form action="" method="post">{% csrf_token %}
        {% for question in page.object_list %}
            <div class="question">
                <h3>{{ question.text }}</h3>
                <div class="choices">
                    {% for choice in question.get_choices_list %}
                        <input type="radio" name="choice-for-question-{{ question.id }}"
                               id="choice-{{ question.id }}-{{ forloop.counter }}" value="{{ choice.id }}"/>
                        <label for="choice-{{ question.id }}-{{ forloop.counter }}">{{ choice.text }}</label><br/>
                    {% endfor %}
                </div>
            </div>
        {% endfor %}
    {% if not page.has_next %}
        <button class='accordion' type="submit">Submit Quiz</button>
    {% endif %}
    </form>
    <div class="pagination">
    <span class="step-links">
        {% if page.has_previous %}
            <a href="?page={{ page.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ page.number }} of {{ page.paginator.num_pages }}.
        </span>

        {% if page.has_next %}
            <a href="?page={{ page.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>
{% endblock %}

0 个答案:

没有答案