如何将表格中的值用作表格烧瓶的一部分

时间:2019-04-19 14:28:14

标签: flask flask-sqlalchemy

我正在制作一个测验应用程序,我试图让用户从“ multiplechoices”选项中选择回来。

我试图从表单中返回该值,而我只是获得了用户选择的选项,例如“选项a”。

我有一个选择表,用于存储用户可以从中选择的所有选项:

class ComputerSystemsMultipleChoices(db.Model):

    __tablename__='computersystemsmultiplechoices'

    id=db.Column(db.Integer, primary_key=True)
    answer=db.Column(db.String(100), unique=True, nullable=False)
    wrong_answer1=db.Column(db.String(100),unique=True, nullable=False)
    wrong_answer2=db.Column(db.String(100),unique=True, nullable=False)
    computersystemsquestions_id=db.Column(db.Integer, db.ForeignKey('computersystemsquestions.id'))

我查询了表格以获取值:

@app.route("/computersystems", methods=['GET','POST'])
def computersystems():
    form=QuizForm()
    computersystemsq=ComputerSystemsQuestions.query.filter(ComputerSystemsQuestions.id).all()
    computersystemsm=ComputerSystemsMultipleChoices.query.filter(ComputerSystemsMultipleChoices.id).all()
    return render_template('computersystems.html',computersystemsq=computersystemsq,computersystemsm=computersystemsm, form=form)

html:

<form action="http://localhost:5000/info" method='POST'>
                {% for computersystemsq in computersystemsq %}
                {% for computersystemsm in computersystemsm %}

                    <div class="row">
                            <div class="col s12">
                                {{ computersystemsq.question }}
                            </div>
                    </div>
                    <div class="{{ computersystemsm.id }}"></div>
                            <div class="row">
                                <div class="col s6">
                                    A: {{ computersystemsm.wrong_answer1 }}
                                </div>
                                <div class="col s6">
                                    B: {{ computersystemsm.wrong_answer2 }}
                                </div>
                            </div>
                            <div class="row">
                                <div class="col s6">
                                    C: {{ computersystemsm.answer }}
                                </div>
                            </div>
                                <div class="row">
                                        <div class="input-field col s6">
                                            {{ form.options( class= computersystemsm.id ) }}
                                            <label for="options">Select an Option:</label>                  
                                        </div>
                                </div>
                                <button class="btn waves-effect waves-light" type="submit" id="{{ computersystemsm.id }}">mark</button>
            <br>
                    </div>  
                {% endfor %}
                {% endfor %}





        </form>

这是测验形式:

class QuizForm(FlaskForm):
    options=SelectField('options', choices= [('option a', 'A'), ('option b', 'B'), ('option c', 'C')])

我尝试返回值的代码:

elif (request.method == 'POST'):
        value = form.options.data
        computer = ComputerSystemsMultipleChoices.query.filter( ComputerSystemsMultipleChoices.id).all()
        quizscore = 0
        if computer[0].answer == value:
            quizscore = quizscore + 1
            return render_template('info.html', quizscore=quizscore,computersystemsm=computersystemsm)
        else:
            return abort(500)

我要返回给我的是存储在表格中的实际选择,例如如果用户选择了C,我想找回{{computersystemsm.answer}},而不是“选项c”。

0 个答案:

没有答案