所以我是 Flask 的新手,正在尝试使用 Flask 和 mongoDB 制作一个图书馆网站,但每次我搜索书籍并尝试单击结果以获取有关该书的更多信息时,它似乎都没有调用该函数。我正在尝试获取附带结果的图书 ID,因此我尝试将其隐藏在不会显示的 div 中。
这是main.py文件
from backend import mongo
from flask import Flask, render_template, request
try:
app = Flask(__name__)
allbooks = mongo.allbooks
rentedBookds = mongo.rentedBooks
userDB = mongo.userDB
@app.route("/")
def home():
return render_template('index.html')
@app.route('/search',methods=['POST','GET'])
def search():
try:
if request.method == 'POST':
search_data = request.form['search_data']
readResult = mongo.read(allbooks,search_data,"title")
return render_template('result.html',content=readResult,specific_value="title")
except KeyError as e:
print(f"Key Error more info here: {e}")
@app.route('/view_book',methods=['POST','GET'])
def book(bookID):
try:
print("hello world")
if request.method =='POST':
bookIDdata = request.args.get(['bookID'])
readResult = mongo.read(allbooks,bookIDdata,"bookID")
return render_template('book.html',BookName=readResult)
except Exception as e:
print(f"There was an error{e}")
if __name__ == "__main__":
app.run()
except TypeError as e:
print(f"There was a type error please check the {e} for the correct type")
except TimeoutError:
print("Something timedout")
except Exception as e:
print(f"There was an error {e}")
这是显示书籍结果的html文件
<html>
<head>
<title>Library Home</title>
</head>
<body>
<form action="/search",method="post">
<input type="text" name="search_data"> </input>
<button id="submit">submit</button>
</form>
<br>
{% for x in content %}
<form action="/view_book" method="post"></form>
<div name="bookID" style="display: none;">
{{x[bookID]}}
</div>
<button id="submit">{{x[specific_value]}}</button>
</form>
{% endfor %}
</body>
</html>
<form action="/view_book/{{x['bookID']}}" method="post">
<button id="submit" type="submit">{{ x[specific_value] }}</button>
</form>
原来还有一份额外的表格