这里我用python、flask、html创建了一个小测试。我无法检查/给出结果。我的问题和答案列表存储在一个名为“问题”的sql数据库中。
这是我在问题页面上打印问题列表的代码。html。(quick.html是我让用户输入多少QN/类别的地方。
但我不知道如何在问答节目中标出哪些是错误的/正确的。我创建了一个名为check的新html页面。html。我试着在quizquestions中循环每个问题,但问题是/questionpage路由器与/QuizQuestion是不同的路由器,因此我无法评估变量quizquestions。看看代码中的B。
A.
@app.route("/quiz", methods=["GET", "POST"])
def quiz():
if request.method == "POST":
chosencategory = request.form.get("chosencategory")
number = int(request.form.get("number"))
quizquestions = db.execute("SELECT Question FROM questions WHERE Category = ? ORDER BY RANDOM() LIMIT ?", chosencategory, number)
return render_template("questionpage.html", quizquestions = quizquestions)
else:
genres = db.execute("SELECT DISTINCT Category FROM questions ORDER BY Category")
return render_template("quiz.html", genres = genres)
B.
@app.route("/questionpage", methods=["GET", "POST"])
def questionpage():
if request.method == "POST":
scores = 0
correct = 0
for quizquestion in quizquestions:
questionline = quizquestion.Question
modelanswers = db.execute("SELECT Answer FROM questions WHERE Question = ?", questionline)
answer = request.form.get("answer").capitalise
if answer == modelanswers:
score += 1
correct = 1
totalscore = scores
return render_template("check.html", quizquestions = quizquestions, modelanswers = modelanswers)
else:
return render_template("questionpage.html")
用于测试/测验的html
<form action="/quiz" method="post">
<div class="form-group">
<div class="form-group">
<select name = "chosencategory">
<option disabled selected value="">Category</option>
{% for genre in genres %}
<option value = "{{genre.Category}}">{{genre.Category}}</option>
{% endfor %}
</select>
</div>
</div>
<input autocomplete="off" autofocus class="form-control" name ="number" placeholder="Number of questions" type="text">
</div>
<p></p>
<div class="form-group">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>
html for/questionpage
<form action="/questionpage" method="post">
<div class="form-question">
{% for quizquestion in quizquestions %}
<p>
<p name = question class = "questionline">{{quizquestion.Question}}</p>
<p></p>
<input autocomplete="off" autofocus class="form-control" name ="answer" placeholder="Answer" type="text">
</p>
{% endfor %}
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
您可以将用户必须回答的当前问题存储在类似于会话的对象中。为了获得更多的参考,我发现了这个线程:跨路径使用变量
我还建议您在SQL查询中使用类似于sqlalchemy的ORM,因为这大大简化了您在这方面的工作。