我正试着用烧瓶做一个语言学习测验,我真的很难找到答案,因为路线的执行方式
@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请求从顶部运行路由代码时,它会选择与显示的图像不同的新图像,并根据用户的回答检查这一点,这意味着用户总是会看到错误的图像。我试过很多事情来解决这个问题,但是似乎没有什么效果,有什么想法吗?
(预先谢谢:)
发布于 2022-04-19 21:39:05
我不确定,但每次你的网站重新加载它会产生新的图像。当你发布的时候,它会通过上面的代码。您传递图像以呈现模板,因此尝试使表单发送到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}}">
https://stackoverflow.com/questions/71931234
复制相似问题