I need to pass parameter to routes using url_for()
function and redirect()
I think I did the same. But, I get TypeError: book() missing 1 required positional argument: 'book_title'
I know that the parameter book_title
is not being recieved by the book()
function in my code and thats why the error. But, I dont know what's going wrong behind the scenes.
These are my routes
@app.route('/search/<title>/',methods=['GET','POST'])
def btitle(title):
book_title = db.execute("SELECT title,author,isbn from books WHERE (title LIKE :title)",params={"title":title}).fetchall()
if request.method == 'GET':
#book_title = db.execute("SELECT title,author,isbn from books WHERE (title LIKE :title)",params={"title":title}).fetchall()
if book_title:
return render_template("booktitle.html",book_title=book_title)
else:
return render_template("error.html")
else:
#book_title = db.execute("SELECT title,author,isbn from books WHERE (title LIKE :title)",params={"title":title}).fetchall()
if book_title:
return redirect(url_for("book",book_title=book_title))
@app.route('/books',methods=['GET','POST'])
def book(book_title):
if request.method == 'GET':
return render_template("individualbook.html",book_title=book_title)
And, this is my booktitle.html
{% extends "layout.html" %}
{% block title %}
{{ book }}
{% endblock %}
{% block body %}
<h1>Search results</h1>
<ul>
{% for book in book_title %}
<li>
<a href="{{ url_for('book') }}">
{{ book }}
</a>
</li>
{% endfor %}
</ul>
{% endblock %}