【发布时间】:2016-03-31 14:08:03
【问题描述】:
我是新手。我不明白为什么该应用程序似乎无法保留随机生成的数据。 get_question() 返回一个带有 2 个键:值对的字典。问题/答案是从此函数随机生成的。每次调用 flash() 时都会重新运行 index() 中的所有代码吗?当闪烁的消息出现时,用户的答案与输入的内容相关,但问题和正确答案似乎是随机出现的——这表明每次单击提交时都会调用整个 index()。我该如何防止这种情况发生?我应该使用“会话”吗?
更新:更具体地说-我不明白为什么闪现的消息与浏览器中呈现的内容完全无关。似乎 flash() 正在运行它自己对我的 get_question() 函数的调用,因此对显示的问题/答案会有所不同?
Here are some images that show the problem
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
user = {'nickname': 'test'} # fake user
question = question_gen.get_question()
answer = AnswerQuestionForm()
q = question['question']
correct = question['answer']
response = ""
if answer.validate_on_submit():
you_said = request.form['answer']
print("You said {}, to the question {}. correct was {}".format(you_said, q, correct))
flash("The question was: %s" % q)
flash("The correct answer was: %s" % correct)
flash("You said: %s" % you_said)
return redirect(url_for('index'))
return render_template('index.html',
title='Home',
user=user,
question=question,
answer=answer,
response=response)
【问题讨论】: