将表单内外的数据保存到数据库中

时间:2019-08-23 14:54:15

标签: javascript python django

我正在学习使用Django进行编码。因此,我有一个显示测验的表单和一个计算这些测验的JavaScript文件。 测验显示在<form>中,我还有其他详细信息,例如课程仍显示在表单上。 据我所知,POST仅采用表格中的数据。如果我在表格之外获得分数,该怎么办?

这是我已经做的:

用于计算结果的JavaScript:

function submitAnswers(answers) {

var total = answers.length;
var score = 0;
var choice = []

//new dynamic method 1
for (var i = 1; i <= total; i++) {

    choice[i] = document.forms["quizForm"]["q" + i].value;
}


for (i = 1; i <= total; i++) {
    if (choice[i] == null || choice[i] == '') {
        alert('You skipped question ' + i);
        return false;
    }
}


// new dynamic method for checking answer
for (i = 1; i <= total; i++) {
    if (choice[i] == answers[i - 1]) {
        score++;
    }
}

//Display Result
var results = document.getElementById('results');
results.innerHTML = "<h3>You scored <span>" + score + "</span> out of <span>" + total + "</span></h3>"
alert("You scored " + score + " out of " + total);

var scoreOnly = document.getElementById('scoreOnly');
scoreOnly.innerHTML = score;

document.getElementById('btnSubmit').style.display = 'none';

return false;
}

包含问题,答案,课程和分数的HTML:

//Gets its value after JavaScript has finished computing.
<div class="text-blue text-darken-3" id="results"></div>
//This div scores only the score. No additional information.
                    <div class="text-blue text-darken-3" id="scoreOnly" name="scoreOnly" hidden></div>
                    <hr>
                    <form method="POST" name="quizForm" action="/quizzer/"
                        onsubmit="return submitAnswers(answers = [{% for q in questn %}'{{ q.answer }}',{% endfor %}])">
                        {% csrf_token %}
                        <div name="canditate">{{user.email}}</div>
                        {% for q in questn %}
                       <div name="course">{{q.course}}</div>
                        <h5> {{ q.question|safe }} </h5>
                        <p><label><input class="with-gap" name="q{{ q.id }}" type="radio" value="a" /><span
                                    class="blue-text text-darken-2"> {{ q.option_a|safe }}</span></label></p>
                        <p><label><input class="with-gap" name="q{{ q.id }}" type="radio" value="b" /><span
                                    class="blue-text text-darken-2"> {{ q.option_b|safe }}</span></label></p>
                        <p><label><input class="with-gap" name="q{{ q.id }}" type="radio" value="c" /><span
                                    class="blue-text text-darken-2"> {{ q.option_c|safe }}</span></label></p>
                        <br>
                        <hr>
                        {% endfor %}
                        <br>
                        <center><button class="btn blue darken-3" type="submit" id="btnSubmit">SUBMIT</button></center>
                    </form>

我尝试的代码:

def index(request):
    if request.method == "GET":
        context = {}
        questn = Questions.objects.all()
        context['title'] = 'Exams'
        context['questn'] = questn
        return render(request, "quizzer/index.html", context)

    if request.method == "POST":
        # print(request.POST)
        course = request.POST["course"]
        scoreResult = request.POST["scoreOnly"]

        theScores = ExamScores(course=course, scoreResult=scoreResult)
        theScores.save()
        return render(request, "quizzer/index.html", context)

我想要的是一种将<form>内外的项目都发布到数据库中的方法。

1 个答案:

答案 0 :(得分:0)

您有两种解决方法。

第一:使用ajax进行发布请求。

second:可以将表单属性添加到放置在表单外部的输入。您可以here获得它。

但是:我希望您需要计算服务器端测验的分数。因为在安全性和正确的逻辑方法方面。