0

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>
4
  • When this happens, does your browser show /view_book in the url bar? Commented Apr 22 at 22:29
  • Where are you expecting the bookID parameter to come from? The route that calls book() 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. Commented Apr 22 at 22:42
  • No it only shows the result html file it doesn't go to a new page or anything
    – AVD
    Commented Apr 23 at 0:20
  • That looks like there is an extra </form>
    – Joe
    Commented Apr 23 at 5:51

1 Answer 1

0
        <form action="/view_book/{{x['bookID']}}" method="post">
            <button id="submit" type="submit">{{ x[specific_value] }}</button>
        </form>

Turns out there was an extra form

Not the answer you're looking for? Browse other questions tagged or ask your own question.