Python Forum
Displaying search results - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Displaying search results (/thread-27661.html)

Pages: 1 2 3 4 5 6 7 8 9


Displaying search results - card51shor - Jun-16-2020

Hey guys so now I'm on the next stage of my project that I've been slaving over thanks to you guys!

I now have it where it searches the database and displays the results to the terminal.

What's the best way to display the results to the website with a link for each one where the user can click and get more information on that item?

I would think I can create HTML elements with Python but I looked into it and it looks like it would be way more complicated then perhaps is necessary.

Here is my code so far. Any ideas on what I should do to best display the search info? Thanks!

@app.route("/search", methods=["GET", "POST"])
def search():
    search = request.form.get("search")
    search1 = search + '%'
    b = db.execute("SELECT * FROM books WHERE author LIKE :search OR isbn LIKE :search OR title LIKE :search LIMIT 10", {'search': search1}).fetchall()
    print(b)
    return render_template("search.html")
            



RE: Displaying search results - ndc85430 - Jun-16-2020

You need to pass the data to your template, don't you? See the docs here. I'd suggest at least trying the example there and trying to understand how it works. If you don't like the official docs, then Miguel Grinberg has a Flask tutorial here (he also has a book on Flask and I imagine a lot of the content is the same).

Also, your variable names aren't very meaningful - use names that tell you what the thing they're naming is for, because that helps you read and understand the code (search1 and b are not good names).


RE: Displaying search results - card51shor - Jun-16-2020

good idea about the variables i will change those.

Please bear with me - I'm not that good at reading API like that it overwhelms me.

I looked at every one - i can't see where any of those coorespond to what I'm saying. I still have no idea how to post a message into the page and not the output.

One of the entries mentions flash(). I used it and it does nothing. So I tend to have more questions raised than answered by these.

I'm way more hands on.

Thanks for helping!!


RE: Displaying search results - ndc85430 - Jun-16-2020

(Jun-16-2020, 06:06 AM)card51shor Wrote: I still have no idea how to post a message into the page and not the output.

I don't understand what this means. What do you mean by "not the output"? The way it works usually is your templates use variables for, well, the variable data and render_template takes the name of the template, as well as the variable names and the data to be used for them and it produces the final HTML that's sent to the client.

From a quick look at the docs, flash seems like it's more about passing messages between requests. If what you want to do is really just put some data on the page, then flash doesn't seem appropriate and you should just stick to the basic templating stuff as above.


RE: Displaying search results - card51shor - Jun-16-2020

OK I guess flash isn't a good option for what I'm doing - it doesn't work for me anyways.

But right now I am printing out the results of my search but it is just going to my terminal. I want it to actually go to the user where they can see the results of their search on the page. I'm not sure how to do that and it seems very complex when I try it alone.


RE: Displaying search results - ndc85430 - Jun-16-2020

Right, so you need to figure out how to work with templates. If you've tried something, why don't you show that?


RE: Displaying search results - card51shor - Jun-16-2020

@app.route("/search", methods=["GET", "POST"])
def search():
    search = request.form.get("search")
    search1 = search + '%'
    b = db.execute("SELECT * FROM books WHERE author LIKE :search OR isbn LIKE :search OR title LIKE :search LIMIT 10", {'search': search1}).fetchall()
    print(b)
    return render_template("search.html")



RE: Displaying search results - ndc85430 - Jun-16-2020

That looks like the same code from the first post and doesn't pass anything to render_template. It'd also be helpful to see your template, don't you think?


RE: Displaying search results - card51shor - Jun-16-2020

sorry i just figured it was working with templates somewhat. I don't see anything on the page you sent me that shows how to do it.

Here is my template:

<!DOCTYPE html>
<html>
  <head>
    <title>Search Bookstore</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="static/bootstrap.min.css" rel="stylesheet" media="screen">
  </head>
  <body>
    <div class="container">
      <p class="login">Logged in. <a href="register">Log out</a></p>
      
       <div class="topnav">
        
        <form
       name="frmInfo3"
       action="/search"
       method="post"     >
  
  <input type="text" size=50% placeholder="Search Books by ISBN, Title, or Author" id="search" name="search">
  <input type="submit" id="searcher" value="Shrekker" />
  </form>
</div> 
      
    </div>
        </form>
  </body>
</html>



RE: Displaying search results - ndc85430 - Jun-16-2020

For a start, your template doesn't have any variables in it, does it? Did you even try the example from the Flask docs like I suggested? It shows you all the pieces - the template and passing variable data to it. OK, the example is a bit different, since the data are passed in the request URL, but that's really not the point. It's just some data that's passed to render_template and the final HTML is generated from that.

How have you been learning how to use Flask, or build a web application in general? It seems like there are a lot of gaps. Maybe try going through Grinberg's tutorial, because I think he explains quite a lot.