0

This is my python code that is the route for a search bar I'm making for a Library database. It always runs the first step, but even after a user submits a search, it does not go to next step. I would like for after the user submits a search, the step will change in the request.form and then it will run the second step. However, in the debugger, it is never showing "getting here". It is evident that the code is never reaching the second step. I'm imagining it has something to do with my request.form not registering the new step.

@app.route('/browse',methods=['get','post'])
def Browse():
    debug("form data=" + str(request.form))
    if "step" not in request.form:
        db = get_db()
        cursor = db.cursor()
        cursor.execute('select booktitle, genre, datepublished, authorfirst, authorlast from book natural join author natural join writes')
        rowlist = cursor.fetchall()
        return render_template("browse.html", step="display_books", entries=rowlist)
    else:
        debug("getting here")
        newdb = get_db()
        newcursor = newdb.cursor()
        #get search from form
        newcursor.execute("select booktitle,genre,datepublished,authorfirst,authorlast from book natural join author natural join writes where booktitle=%s", [request.form['searchentry']])
        newdb.commit()
        newrowlist = newcursor.fetchall()
        return render_template("browse.html", step="search_books", newentries =newrowlist)

Here is the html code

<html>
  <head>
    <title>Browse</title>
    <style>
    body {
                background-image: url(https://storage.cloud.google.com/book_lib_img/Bookworm%20Webpage/wallpaper4.jpg?cloudshell=false&folder&organizationId);
                height: 500px;
                background-position: center;
                background-repeat: no-repeat;
                background-size: 100%100%;
                position: absolute;

            }
        h1 {
             color: white;
             font-family: "Comic Sans MS", "Comic Sans", cursive;
             font-size:150%;
             position: absolute; 
             color:white;
             TOP:200px;
             LEFT:520px
            }


        #Add a black background color to the top navigation 
        ul {
             list-style-type: none;
             margin: 0px;
             padding: 0px;
             overflow: hidden;
             background-color: #333;
             position: fixed;
             top:150;
             width: 100%;

            }

        li {
            float: left;
            border-right: 1px solid #bbb;
        }

        li a {
            display: block;
            color: white;
            text-align: center;
            padding: 10px 15px;
            text-decoration: none;
        }


        li a:hover {
            background-color: #111;
        }

</style> 
  </head>
  <body>
    <h1></h1>

    <img src=https://storage.cloud.google.com/book_lib_img/Bookworm%20Webpage/Bookworm.jfif alt="HTML5 Icon" STYLE="position:absolute; TOP:5px; LEFT:5px; WIDTH:100px; HEIGHT:100px">
    <ul>
    <li><a class="active" href="/">Home</a>
    <li><a href="/browse">Select Preference</a>
    <li><a href="/edit">My Account</a>
    <li><a href="/signup">Create Account</a>
    <li><a href="/login">Login</a>
    <ul>
    {% if step == "display_books" %}
        <h2>Browse entries </h2>
            <form style="color:white;background-color:gray;padding-left: 40px;border="1"">
                <form action="{{ url_for('Browse') }}" method="get">
                <h3>Search by:</h3>
                <input type="radio" name="title" value="Title" checked> Title <br>
                <input type="radio" name="author" value="Author">Author
                <input type="text" style="width:300px" name="searchentry" placeholder="Search for...">
                <input type="radio" name="genre" value="Genre">Genre<br>
                <input type="hidden" name="step" value="search_books">
                <input type = "submit">
            </form>
            <table cellspacing="0" cellpadding="0" border="0" width="700">
                <tr>
                    <td>
                        <table cellspacing="0" cellpadding="1" border="1" width="700">
                            <tr style="color:white;background-color:grey">
                                <th>Title</th>
                                <th>Author</th>
                                <th>Published</th>
                                <th>Genre</th>
                            </tr>
                        </table>
                     </td>
                </tr>
                <tr>
                    <td>
                        <div style="width:700px; height:340px; overflow:auto;">
                            <table cellspacing="0" cellpadding="1" border="1" width="700">
                                {% for entry in entries %}
                                    <tr style="color:white;background-color:grey">
                                        <td>{{entry.booktitle}}</td>
                                        <td>{{entry.authorfirst+" "+entry.authorlast}}<br>
                                        <td>{{entry.datepublished}}</td>
                                        <td>{{entry.genre}}</td>
                                    </tr>
                                {% endfor %}
                            </table>
                        </div>
                    </td>
                </tr>
            </table>
    {% else %}

        <h2>Your Search Results</h2>
            <form style="color:white;background-color:gray;padding-left: 40px;border="1"">
                <form action="{{ url_for('Browse') }}" method="get">
                <h3>Search by:</h3>
                <input type="radio" name="title" value="Title" checked> Title <br>
                <input type="radio" name="author" value="Author">Author
                <input type="text" style="width:300px" name="searchentry" placeholder="Search for...">
                <button type="button">Search</button><br>
                <input type="radio" name="genre" value="Genre">Genre<br>
                <input type="hidden" name="step" value="search_books">
                <input type="submit">
            </form>
            <table cellspacing="0" cellpadding="0" border="0" width="700">
                <tr>
                    <td>
                        <table cellspacing="0" cellpadding="1" border="1" width="700">
                            <tr style="color:white;background-color:grey">
                                <th>Title</th>
                                <th>Author</th>
                                <th>Published</th>
                                <th>Genre</th>
                            </tr>
                        </table>
                     </td>
                </tr>
                <tr>
                    <td>
                        <div style="width:700px; height:340px; overflow:auto;">
                            <table cellspacing="0" cellpadding="1" border="1" width="700">
                                {% for newentry in newentries %}
                                    <tr style="color:white;background-color:grey">
                                        <td>{{newentry.booktitle}}</td>
                                        <td>{{newentry.authorfirst+" "+newentry.authorlast}}<br>
                                        <td>{{newentry.datepublished}}</td>
                                        <td>{{newentry.genre}}</td>
                                    </tr>
                                {% endfor %}
                            </table>
                        </div>
                    </td>
                </tr>
            </table>
    {%endif%}
    </body>
</html>

1 Answers1

0

As per Get the data received in a Flask request, it appears you are trying to get the form data when looking in request.form, which contains form data from HTTP POST requests, but really, you are looking for the key-value arguments in the URL.

To get these arguments, instead of using request.form, use request.args.

dddJewelsbbb
  • 626
  • 4
  • 17