Python Flask:返回MCQ输出,突出显示正确和选择的答案 [英] Python Flask: Return MCQ output, with right and chosen answers highlighted

查看:56
本文介绍了Python Flask:返回MCQ输出,突出显示正确和选择的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个烧瓶应用程序:

I have this flask app:

import os
import random
from flask import Flask, render_template, request
app = Flask(__name__)

quiz_host = os.environ.get('FLASK_HOST')
quiz_port = os.environ.get('FLASK_PORT')

questions = [{'id':1, 'question':'what is 2 + 3','correct':'2','choices':['1','2','3',5']}, {'id':2,'question':'what is 4 + 6',correct':'10','choices':['10','11','12','13']}]

@app.route("/quiz", methods=['POST', 'GET'])
def quiz():
    if request.method == 'GET':
        return render_template("index.html", data=questions)
    else:
        result = 0
        total = 0
        for question in questions:
            if request.form[question.get('id')] == question.get('correct'):
                result += 1
            total += 1
        return render_template('results.html', total=total, result=result)


if __name__ == "__main__":
    app.run(host=quiz_host, port=quiz_port)

现在,您可以在结果页面上看到它只打印总数和结果.我希望结果页将所有MCQ的所有问题打印在index.html页上,突出显示学生单击的问题,正确的答案是,以便学生可以看到他们是对还是错(因为现在,结果页面上只会显示您正确地获得了5/11个答案").

Right now you can see, on the results page, it just prints the total, and the results. I would like the results page to instead print all of the questions on the index.html page, for each MCQ, highlighting the question the student clicked, and the right answer was, so the student can see what they got right and wrong (because right now, the results page just says 'you got 5/11 answers right').

我不确定该怎么做.我猜(1)我需要在上面的脚本中编辑"else"循环,(2)我需要更改从函数返回的内容.我一直在专心做事,网上找不到任何东西.有人可以帮忙吗?

I'm not sure how to do this. I guess (1) i need to edit the 'else' loop in the above script, (2) i need to change what is returned from the function. I'm stuck on how to do it specifically and I can't find anything online about it. Can anyone help?

我将此添加到了results.html页面:

I added this to the results.html page:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Junior Cert Random Question Generator</title>
        <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='styles/stylesheet_results.css') }}">
        <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
    </head>
    <body>
        <div>
            <h1>Your Results!</h1>
            <br>
            <h2>You scored {{ result }} out of {{ total }}!</h2>

                {% for question in data %}
                <h3>{{ question.question }}</h3>

                {% for answer in question.answers %}
                <input type="radio" id="{{ answer }}" name="{{ question.id }}" value="{{ answer }}"> {{ answer }}<br>
                {% endfor %}
                {% endfor %}    
        </div>
    </body>
</html>

现在,问题将复制到results.html页面上.

So now the questions are replicated on the results.html page.

推荐答案

您只需在模板中使用jinja语法即可.在其他部分,因为您仅返回具有total和result的模板,但还应添加问题就像您做的部分一样,然后您知道正确的答案,您可以突出显示该内容.然后通过request.form.get('user_response')获得用户的响应,其中'user_response'是输入字段的名称,其中他可以提交答案.然后再次使用jinja突出显示该内容,方法是像使用total和result一样,使用变量将其传输到模板中.

You can simply do that using jinja syntax in your template.In your else part as you are returning a template only with total and result but you should also add questions as you did in if part and then as you know the right answer, you can highlight that.Then get the response of the user by request.form.get('user_response') where 'user_response' is the name of the input field where he can submit the answer.Then again using jinja highlight that by transferring that to your template using variable like you did with total and result.

这篇关于Python Flask:返回MCQ输出,突出显示正确和选择的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆