-1

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 %}
Sharik Sid
  • 109
  • 2
  • 9

1 Answers1

0

You problem is that the book route is not getting the argument book_title that it expects.

This is because you are defining it like this:

@app.route('/books',methods=['GET','POST'])
def book(book_title)

In flask, if you want your view functions to take parameters you need to include them in the route. In your example this could look like this:

@app.route('/books/<book_title>',methods=['GET','POST'])
def book(book_title)

If you do not put the <book_title in the route, flask will not be able to provide the book_title parameter to the book function which is what it tells you in the error.

k-nut
  • 3,447
  • 2
  • 18
  • 28
  • If I do that I get this `werkzeug.routing.BuildError: Could not build url for endpoint 'book'. Did you forget to specify values ['book_title']?` on `return render_template("booktitle.html",book_title=book_title)` – Sharik Sid Jul 31 '18 at 15:02