我如何在 Flask 中进行测验?
How do I make a quiz in Flask?
我正在尝试在 Flask 中进行语言学习测验,但由于路由的执行方式,我真的很难找到有效的答案
@views.route('/question', methods=['GET','POST'])
def question():
HorK = session.get("HorK", None)
images = []
lessonID = session.get("lessonChoice", None)
quizImages = getQuizImages(lessonID, images)
image = random.choice(quizImages)
# this part essentially generates a random image from a set of characters and then the user has to write down the correct sound of that character
# don't worry too much about the variables but the html page needs HorK and an image to run
if request.method == 'POST':
character = Character.query.filter_by(image=image).first()
answer = request.form.get("answer")
if answer.upper() == character.romaji:
flash('Correct', category='success')
else:
flash('Incorrect', category='error')
return render_template("question.html", HorK=HorK, image=image)
我认为问题在于,当发出 post 请求时,它会从顶部运行路由代码,因此它会选择与显示的图像不同的新图像,并根据用户的回答进行检查,这意味着用户将总是看错图像。我已经尝试了很多方法来解决这个问题,但似乎没有任何效果,有什么想法吗?
提前致谢:)
我不确定,但每次您的网站重新加载时它都会生成新图像。当你 POST 它会通过上面的代码。您将图像传递给渲染模板,因此请尝试让您的表单也发送您已传递给 html 文件的图像,并从请求中获取图像。您甚至无需在网站上显示任何输入字段即可做到这一点。
@views.route('/question', methods=['GET','POST'])
def question():
HorK = session.get("HorK", None)
images = []
lessonID = session.get("lessonChoice", None)
quizImages = getQuizImages(lessonID, images)
# this part essentially generates a random image from a set of characters and then the user has to write down the correct sound of that character
# don't worry too much about the variables but the html page needs HorK and an image to run
if request.method == 'POST':
character = Character.query.filter_by(image=request.form.get("image")).first()
answer = request.form.get("answer")
if answer.upper() == character.romaji:
flash('Correct', category='success')
else:
flash('Incorrect', category='error')
image = random.choice(quizImages)
return render_template("question.html", HorK=HorK, image=image)
记得在你传递图片的地方添加形成不可见输入
这里是例子<input type="hidden" name="image" value="{{image}}">
我正在尝试在 Flask 中进行语言学习测验,但由于路由的执行方式,我真的很难找到有效的答案
@views.route('/question', methods=['GET','POST'])
def question():
HorK = session.get("HorK", None)
images = []
lessonID = session.get("lessonChoice", None)
quizImages = getQuizImages(lessonID, images)
image = random.choice(quizImages)
# this part essentially generates a random image from a set of characters and then the user has to write down the correct sound of that character
# don't worry too much about the variables but the html page needs HorK and an image to run
if request.method == 'POST':
character = Character.query.filter_by(image=image).first()
answer = request.form.get("answer")
if answer.upper() == character.romaji:
flash('Correct', category='success')
else:
flash('Incorrect', category='error')
return render_template("question.html", HorK=HorK, image=image)
我认为问题在于,当发出 post 请求时,它会从顶部运行路由代码,因此它会选择与显示的图像不同的新图像,并根据用户的回答进行检查,这意味着用户将总是看错图像。我已经尝试了很多方法来解决这个问题,但似乎没有任何效果,有什么想法吗?
提前致谢:)
我不确定,但每次您的网站重新加载时它都会生成新图像。当你 POST 它会通过上面的代码。您将图像传递给渲染模板,因此请尝试让您的表单也发送您已传递给 html 文件的图像,并从请求中获取图像。您甚至无需在网站上显示任何输入字段即可做到这一点。
@views.route('/question', methods=['GET','POST'])
def question():
HorK = session.get("HorK", None)
images = []
lessonID = session.get("lessonChoice", None)
quizImages = getQuizImages(lessonID, images)
# this part essentially generates a random image from a set of characters and then the user has to write down the correct sound of that character
# don't worry too much about the variables but the html page needs HorK and an image to run
if request.method == 'POST':
character = Character.query.filter_by(image=request.form.get("image")).first()
answer = request.form.get("answer")
if answer.upper() == character.romaji:
flash('Correct', category='success')
else:
flash('Incorrect', category='error')
image = random.choice(quizImages)
return render_template("question.html", HorK=HorK, image=image)
记得在你传递图片的地方添加形成不可见输入
这里是例子<input type="hidden" name="image" value="{{image}}">