我正在嘗試在 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)
我認為問題在于,當發出發布請求時,它會從頂部運行路由代碼,因此它會選擇與顯示的影像不同的新影像,并將其與用戶的答案進行檢查,這意味著用戶將始終查看錯誤的影像。我已經嘗試了很多方法來解決這個問題,但似乎沒有任何效果,有什么想法嗎?
提前致謝 :)
uj5u.com熱心網友回復:
我不確定,但每次您的網站重新加載時,它都會生成新影像。當您發布時,它會通過上面的代碼。您將影像傳遞給渲染模板,因此請嘗試使您的表單也發送您已傳遞給 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://www.uj5u.com/qiye/458977.html
