0

I have nearly finished the Cs50x course but I am stuck on my final project. I am creating a website called 'Letterbooks' (inspired by Letterboxd) that allows you to log books that you have read. I am connecting the program to a SQLite database that is full of around 3000 books.

In my database I also have these tables:

users - stores user information (username, password etc)

logbook - contains the books that the user has 'logged'/read

The problem -

I have successfully completely the 'searched' page where the user can type in a title or author or ISBN number and the results show up.

NOW on the results page, I am trying to code a 'log' button where the user can click on the log button that is next to the book they have read. After clicking the button, the user will be automatically redirected to their homepage and they will see a table/logbook of that book they have just 'logged'.

At the moment when the log button is clicked but I get a 500 error/Runtime error: unused values (book)

Where am I going wrong? Any tips would be appreciated.

{% extends "layout.html" %}

{% block title %}
  Results
{% endblock %}

{% block main %}
    <h1 class="display-4">Search Results</h1>
        <table class="table table-striped">
            <trhead>
                <tr>
                    <th>ISBN</th>
                    <th>Title</th>
                    <th>Author</th>
                    <th>Year</th>
                    <th></th>
                </tr>
            </trhead>

            <!-- table body -->
            <form action="{{ url_for('results') }}" name="book" value="book" method="post">
            <tbody>
                {% for result in results %}
                    <tr>
                        <td>{{result['isbn']}}</td>
                        <td>{{result['title']}}</td>
                        <td>{{result['author']}}</td>
                        <td>{{result['year']}}</td>
                        <td>
                            <button class="btn btn-primary" type="submit">Log</button>     
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
            </form>
        </table>
@app.route("/results", methods=["GET", "POST"])

@login_required

def results():
    """Show Search Results and there's an option to log a book"""

    if request.method == "POST":
        
        book=request.form.get("book")
        
        isbn = db.execute("SELECT isbn FROM books WHERE title=book", book=book)
        title = db.execute("SELECT title FROM books WHERE title=book", book=book)
        author = db.execute("SELECT author FROM books WHERE title=book", book=book)
        year = db.execute("SELECT year FROM books WHERE title=book", book=book)
        
        db.execute("INSERT INTO logbook (user_id, isbn, title, author, year) VALUES (?, ?, ?, ?, ?)",
                    session["user_id"], isbn, title, author, year)
                   

        return render_template("index.html")

    else:
        return render_template("results.html")

Python code

0

The traceback in the flask console will tell you which line is producing the error. Without that info, here are several potential problems:

  • <value> is not a valid attribute for a <form> element. The HTML will not validate.
  • if the <value> attribute defined on the form element is in fact sumbitted with the form, it will always be the string literal "book".
  • The four selects seem to be missing the "named parameter" (eg title=:book)
1
  • Okay thanks. Looking at it, I think my real question is what is the definition of the 4 selects i.e isbn, title, author, year. I think the book = request.form.get("book") must be wrong way to go about defining them?
    – H S
    Jun 3 '21 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .