aarushi777
aarushi777

Reputation: 1

why isn't my route taking me to where I want to go in Flask with the correct parameter?

So on my route displayinfo I am displaying information, so I made the request method GET, and additionally on my html page I have a form that asks for a user review, so I made the request method post. but, when I click the button to submit the form information it takes me to displayinfo/, but I want it to take me to displayinfo/(isbn number)

this is my python code

@app.route("/displayinfo/<book_isbn>", methods=["POST", "GET"])

def displayinfo(book_isbn):
    #global Book_isbn
    #Book_isbn = book_isbn
    if request.method == "POST":

        if not session.get("USERNAME") is None:
            username = session.get("USERNAME")

            review = request.form.get("review")

            user_id = db.execute("SELECT user_id FROM users WHERE username=:username", {"username": username}).fetchone()
            book_id = db.execute("SELECT book_id FROM books WHERE isbn=:isbn)", {"isbn": book_isbn}).fetchone()

            db.execute("INSERT INTO reviews (comment, user_id, book_id) VALUES (:comment, :user_id, :book_id)",{"comment": review, "user_id": user_id, "book_id":book_id})


            db.commit()

        else:
            return render_template("error.html", printthis="Not signed in.")

    else:

        if not session.get("USERNAME") is None:

            booktitle = db.execute("SELECT title FROM BOOKS WHERE isbn = :isbn", {"isbn": book_isbn}).fetchone()
            bookauthor = db.execute("SELECT author FROM BOOKS WHERE isbn = :isbn", {"isbn": book_isbn}).fetchone()
            bookyear = db.execute("SELECT year FROM BOOKS WHERE isbn = :isbn", {"isbn": book_isbn}).fetchone()
            bookisbn = db.execute("SELECT isbn FROM BOOKS WHERE isbn = :isbn", {"isbn": book_isbn}).fetchone()

            #res = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": KqqGbJlg2XMOyLiYRAnhvQ, "isbns": bookisbn})

            return render_template("displayinfo.html", printtitle=booktitle, printauthor=bookauthor,printyear=bookyear, printisbn=bookisbn, book=booktitle)

            db.commit()
        else:
            return render_template("error.html", printthis="Not signed in.")

and this is my html code

{% extends "template.html" %}
{% block title %}Bookstore{% endblock %}

{% block head %}{{ printtitle }}{% endblock %}

{% block body %}
{{ printtitle }}
{{ printauthor }}
{{ printyear }}
{{ printisbn }}





<p>Write a review: </p>

<form action="{{ url_for('displayinfo',  book_isbn=book.isbn)}}" method="post">

<!--, book_isbn=book.isbn-->

    <li><input type="text" name="review" placeholder="Write a review for {{ book }}"></li>
    <div class="btn">
    <button>Submit</button>
    </div>


</form>




{% block text %}{{ review }}{% endblock %}

{% endblock %}

Upvotes: 0

Views: 53

Answers (2)

Nandakishore
Nandakishore

Reputation: 11

The book variable passed to the template from Flask doesn't seem to be a book object but rather a string with it's content as the book title.

return render_template("displayinfo.html", printtitle=booktitle, printauthor=bookauthor,printyear=bookyear, printisbn=bookisbn, book=booktitle)

Perhaps you could try fixing it by passing a book object or replacing book.isbn with the isbn string you are passing - bookisbn

On another note, I suggest using a trailing slask at the end of the URL in @app.route. It's better, according to the Flask Quickstart - https://flask.palletsprojects.com/en/1.1.x/quickstart/#unique-urls-redirection-behaviorhttps://flask.palletsprojects.com/en/1.1.x/quickstart/#unique-urls-redirection-behavior

Upvotes: 1

Balaji Ambresh
Balaji Ambresh

Reputation: 5037

After

db.commit()

Paste the line below

return redirect(url_for('displayinfo', book_isbn=book_isbn))

The relevant imports are:

from flask import redirect, url_for

Upvotes: 1

Related Questions