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


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

Aleady explained what you need to do. I really wouldn't cache the search results and share them, for a couple of reasons:

1. It makes the application harder to use. If the user already knows, say, the ISBN of the book and wants to go directly to its page, then they couldn't unless it had been searched for first (because your shared list wouldn't contain it, of course).

2. It makes the application more complex, because you'd have to worry about the data in the cache being stale and refreshing them. Maybe the risk for this small app is minimal, but I think simpler is better right now.

Just do a query in the handler for the book page. It's not like the performance is going to be bad.


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

good point okay i'll post my findings tomorrow, good sir.


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

OK so I finally started coding it a bit. I immediately have the same issue I was talking about.

I can't run another query since there is no click event like the search button to start the search from being run.

Basically what I'm saying is how can I get the result list from clicking the search button into my book function?

I don't think I can do it the way you said - at least not any way I'm aware of. Can you help me out a bit?

Thanks

Here's my code. Obviously the "result" variable is not usable in my book function. How can I change that?

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


@app.route("/book", methods=["GET", "POST"])
def book():
    return render_template("book.html")
HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>{{isbnResult}}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="static/bootstrap.min.css" rel="stylesheet" media="screen">
  </head>
  <body>
    
    <p>ISBN: {{result[0]}}</p>
    <p>Title: {{result[1]}}</p>
    <p>Author: {{result[2]}}</p>
    <p>Year: {{result[3]}}</p>
    <p>Reviews: </p>
    
   
  </body>
</html>



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

You need a variable route now, don't you? Take the ISBN as a path parameter and use that for your query. I thought that's how you were going to make the links from the search page? Again, clicking a link (or typing the URL in the browser and hitting "Enter") will make the HTTP request, so you can put variable data in the URL.


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

how do I get the isbn, though? What am I doing to trigger the event? For instance, for search, it's when I click the search button, then the code in python tells it to execute search.

How do I get it to execute the SQL?

no right now the link is just to the "book" page. I didn't use the isbn since I figured we didn't have to do that with the URL. We have to do it with a JSON request but that's something I'll worry about later.

I have one week left to finish this I'm getting really nervous. All I have to do is figure out how to get the information I already have into this function.

Surely there's got to be an easy way, right?

The way you are explaining has me very confused.

@app.route("/book", methods=["GET", "POST"])
def book():
    title = request.form.get("book")
    author = ""
    year = ""
    reviews = ""
    return render_template("book.html", title=title, author=author, year=year, reviews=reviews)

basically how do i get access to the http event?


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

Your search page lists all the books that match the criteria and the links on that page are meant to take you to the info for an individual book, right? You need a way to identify a book, like the ISBN. Put that data in the URL, so the paths in the URL look like /book/978-0321503626 or /book/978-0134757599. So your handler for /book needs to take the ISBN from the path and put it in the query to look up the data for that book. You already know how to do queries, so there shouldn't be a problem there. I don't know why you think you need JSON either; that's just overcomplicating things.

You must have at least seen variable routes before - if you've ever used Twitter, their URLs are like https://twitter.com/BobSmith or https://twitter.com/AliceJones. I'm sure you can think of other examples, but in any case, the point is, of course this is possible.


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

OK I can do that. How do I get the isbn from the path, however?


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

By using a variable route. Come on, you've seen the docs by now.


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

Not for this specific instance. The user isn't enter the data into the box.


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

(Jun-21-2020, 05:48 AM)card51shor Wrote: Not for this specific instance.

That's just wrong. We mentioned variable routes earlier in this thread. See, e.g. this post. Where did that example come from?

Quote:The user isn't enter the data into the box.

Of course not, because the data goes in the URL. When you submit data in a form, it doesn't go in the URL does it? It's in the request body.