我試圖從quizlist.html 輸入中獲取最后一個條目并將其放在test.html 上。我通過 app.py 綁定但沒有成功,也嘗試從 html 頁面本身但沒有成功。我不是專家,但我認為我的數據保存在會話中,而不是像我一樣保存在 sqlalchemy 中。這不是問題,我只是想從 html 頁面獲取最后一個條目并將其放在另一個 html 頁面上。場景是,當用戶填寫文本輸入時,所有數據(因為用戶可以輸入多次)將作為表格打印在 result.html 上。我只想在 test.html 上打印最后一個條目這是我的 app.py<!DOCTYPE html><html> <head> <script src="https://cdn.jsdelivr.net/gh/linways/[email protected]/dist/tableToExcel.js"></script><script>function exportReportToExcel() { let table = document.getElementsByTagName("table"); TableToExcel.convert(table[0], { name: `Questions & Answers.xlsx`, sheet: { name: 'Sheet 1' } });}</script> </head> <body style="font-family: Arial, Helvetica, sans-serif; background-color: #FFFAFA;"><div style="width:800px; margin:0 auto;"> <h3> <a style="color:#14568B;" href = "{{ url_for('result') }}"> Click to Show Table of Questions and Answers</a> </h3> <hr/> {%- for message in get_flashed_messages() %} {{ message }} {%- endfor %} <h3> (<a style="color:#14568B;" href = "{{ url_for('quickQuizList') }}">Click to Add Quick Quiz Question </a>)</h3> <table style="border: 1px solid black;"> <thead> <tr width="380" style="border: 20px solid black ;"> <th width="80" style="border: 1px solid black ;">TF Question</th> <th width="80" style="border: 1px solid black ;">TF Answer</th> <th width="80" style="border: 1px solid black ;">MCQ Question</th> <th width="80" style="border: 1px solid black ;">MCQ Answer</th> </tr> </thead>
1 回答

aluckdog
TA貢獻1847條經驗 獲得超7個贊
您正在使用從數據庫中檢索所有對象quickQuizQuestions.query.all()
。
您只想檢索一個對象;您的最后一個條目。如果您的問題 ID 確實增加,您可以通過以下方式執行此操作:
quickQuizQuestions.query.order_by(-quickQuizQuestions.id).limit(1).all()
或者
quickQuizQuestions.query.order_by(-quickQuizQuestions.id).first()
添加回答
舉報
0/150
提交
取消