Python Forum
Displaying search results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying search results
#61
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]
Reply
#62
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.
Reply
#63
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!
Reply
#64
(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?
Reply
#65
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.
Reply
#66
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!
Reply
#67
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
Reply
#68
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?
Reply
#69
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!
Reply
#70
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?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020