Python/Flask-WTF-如何从动态RadioField中随机选择? [英] Python/Flask-WTF - How can I randomize the choices from dynamic RadioField?

查看:246
本文介绍了Python/Flask-WTF-如何从动态RadioField中随机选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用python/flask/flask-wtf进行多项选择的测验.我能够成功地从数据库中提取随机问题以及提取随机选择作为可能的答案.我在模板页面上使用一个for循环,该循环首先显示问题,然后显示可能的答案.

I am looking to build a multiple choice quiz using python/flask/flask-wtf. I am successfully able to pull random questions from a database as well as pull random choices as the possible answers. I am using a for loop on the template page that first displays the question and then the possible answers.

这是我的模板.

<div class="quote-block">
{% for quote in quotes %}
    {{ quote.line_text }}
    <form method="POST">
        {{ form.hidden_tag() }}
        {{ form.question }}
        <input type="submit">
    </form>
{% endfor %}
</div>

我的问题是,每个问题的可能答案相同且顺序相同.我了解为什么会这样.查询数据库中的RadioField选项仅发生一次.然后针对for循环的每个实例提取该查询的结果.

My problem is that the possible answers are the same for each question and in the same order. I understand why this is happening. The query into the database for the RadioField choices is only happening once. And then the results of that query are being pulled for each instance of the for loop.

这是我的观看路线.

@app.route('/quiz/', methods=['POST', 'GET'])
def quiz():
    quotes = Quotes.query.order_by(func.rand()).limit(5)
    form = Quiz()
    form.question.choices = [(choice.speaker_id, choice.speaker) for choice in Characters.query.order_by(func.rand()).limit(4)]
    return render_template('television/quiz.html', form=form, quotes=quotes, options=options)

还有我的表格.

class Quiz(FlaskForm):
    question = RadioField('Label', choices=[])

就像我说的那样,所有这些都有效.我只是不知道如何为每个问题启动一个新的选择查询.任何帮助将不胜感激.谢谢!

Like I said, all of this works. I just can't figure out how to start a new choices query for each question. Any help would be greatly appreciated. Thanks!

推荐答案

经过大量的阅读和研究,我意识到我试图过早地做太多事情.因此,我退后了一步,构建了我想做的工作的块,然后最终融合在一起.当然,我在表单验证部分遇到了新问题,但是我将其包含在新问题中.

After a lot of reading and research, I realized I was trying to do too much too soon. So I took a step back and built the blocks of what I was trying to do and then it finally came together. Of course, I am having new issues with the form validation portion, but I'll include that in a new question.

我最大的障碍是我试图为RadioField选择随机选择选项.相反,我在数据库表中添加了三列,并在表中提供了选项.这样一来,通过一个查询就可以轻松提取选项和问题.

My biggest roadblock was that I was trying to pull random choice options for the RadioField. Instead, I added three columns to the database table and supplied the options in the table. That made it easier to pull the options and the question with one query.

基本上,我正在表单中的字段上运行一个for循环.如果该字段是RadioField,它将对数据库运行查询并拉出随机行.然后,我在拉出的行上使用另一个for循环,并将不同的元素分配给RadioField的各个部分(标签,选择项).

Basically, I am running a for loop over the fields in the form. If the field is a RadioField, it runs a query against the database and pulls a random row. I then use another for loop over the pulled row and assign different elements to the pieces of the RadioField (label, choices).

如果您知道执行此操作的更优雅的方法,我很想听听.但是现在它可以了.

If you know of a more elegant way to do this, I'd love to hear it. But for now it works.

我的表单和模板保持不变.但是,这是我的新路线视图.

My form and template stayed the same. But here is my new route view.

    @app.route('/quiz/', methods=['GET', 'POST'])
    def quiz():
        form = Quiz()
        for field in form:
            if field.type != "RadioField":
                continue
            else:
                pulls = Quotes.query.order_by(func.rand()).limit(1)
                for pull in pulls:
                    answer = pull.speaker
                    option_two = pull.option_two
                    option_three = pull.option_three
                    option_four = pull.option_four
                    question = pull.line_text
                    field.label = pull.line_text
                    field.choices = [(answer, answer), (option_two, option_two), (option_three, option_three), (option_four, option_four)]
            return redirect(url_for('you_passed'))
        return render_template('television/quiz.html', form=form)

这篇关于Python/Flask-WTF-如何从动态RadioField中随机选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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