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 - card51shor - Jun-18-2020

Currently getting better results. I am now passing both variables through my template.

It's printing out more than I want though. I'm figuring that out now. Here is my current code:

HTML :

    <ul id="results">
          {% for i in search %}
            {% for j in link %}
            <a href="jack">{{j}}</a>
            <li>{{i}}</li>
            {% endfor %}
          {% endfor %}
      
        </ul>
PYTHON:

@app.route("/search", methods=["GET", "POST"])
@app.route("/search/<isbnResult>")
def search():
    search = request.form.get("search")
    searchCombo = search + '%'
    isbnResult = db.execute("SELECT isbn FROM books WHERE author LIKE :search OR isbn LIKE :search OR title LIKE :search LIMIT 10", {'search': searchCombo}).fetchall()
    result = db.execute("SELECT * FROM books WHERE author LIKE :search OR isbn LIKE :search OR title LIKE :search LIMIT 10", {'search': searchCombo}).fetchall()
    print(result)
    return render_template("search.html", search=result, link=isbnResult)
And here's the output I'm getting...obviously it's iterating over it too many times.

[Image: v.php?i=e91da8d912]


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

For a start, why are you doing two queries and then passing those two sets of results to the template? The ISBN will already be in result, so you're just making life more complicated for yourself. If you tried it with only one query and got errors, for heaven's sake show us what those were.

Also, why are you overcomplicating your routing? You don't need the variable route on line 2, since you're not varying the path in the URL.


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

Because it makes it easier for me to print the results since I wanted only the ISBN to be a link. But now that I think about it, I'll make it all a link and get rid of it.

As for your second complaint - part of the assignment is to make it so if the user puts in search/<isbn> into the url, it takes u to the book page for that isbn.

I'm updating my code - I made some progress - i'll have it up soon. Thanks!


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

(Jun-18-2020, 05:52 AM)card51shor Wrote: Because it makes it easier for me to print the results since I wanted only the ISBN to be a link. But now that I think about it, I'll make it all a link and get rid of it.

That doesn't make any sense. Even if you had the entire row in the template (which you do when you iterate over search in there), you don't have to use it all for the link, just the ISBN value. Why would you think otherwise?


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

I realize I could - but it makes the syntax sloppy and complicated to do that. I tried it. It's easier to do it my way and make the whole thing a link. I'm doing that now.


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

It really isn't sloppy or complicated - you just get the ISBN by whatever index the value is at in the tuple, but obviously if you like overcomplicated code, go for it!


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

Wait if you read anything I just said - disregard it. I don't need to have that isbn url link like I suggested.

All I need to do is have it so when you click on any of the search results, you get a book page with info on the book.

I think I can handle that. I'm going to take a break and try again tomorrow night. Please leave any tips in the meantime! Here is my current code:

PYTHON:

@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")
I'm currently working on book.html and creating the file. One quick question I think I'll have is : How do I pass the value from search.html (with all the search result info I need) into books.html? I assume I pass it through the template just like I did with search? Do I use the same variable name? Does it matter?

Thanks


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

You're not thinking the right way - you don't pass values from some HTML to another. Clicking a link makes an HTTP request to that URL, so put the relevant data in the URL and read it in the handler. By relevant data, I mean whatever will allow you to look up the individual book. Some of this should be obvious though: the HTML is rendered on the client (i.e. the browser) and clearly the data doesn't live there, so you need to go back to the server to fetch it.

Also, why does your handler for /book need to support POST requests?


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

obviously the ISBN is what I'm going to use. But I think I got it I'm going to hop back on it this weekend and I'll update you with what I do.

Thanks!


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

basically what i'm saying is can I use the results variable I get in the search method in the book method?

How do I pass that variable so I can use it in another function/method?