So I'm new to flask and am trying to make a library website with flask and mongoDB but everytime I search for the books and try to click on the result for more information about the book it doesn't seem to call the function. I'm trying to sort of get the book ID of the results that come with it so I'm trying to hide it in a div that won't be shown.
This is the main.py file
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}")
This is the html file that shows the book results
<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>
/view_book
in the url bar?bookID
parameter to come from? The route that callsbook()
is just the static URL/view_book
, there's no place to specify a parameter - and you don't use the parameter in the function anyway.</form>