首页 > 解决方案 > 这里没有做些什么?

问题描述

我已经在这里跟踪了几个星期,尽我所能尝试一切,但没有预期的结果。我想在用户搜索并单击图书链接时显示图书的详细信息。我的搜索路线工作正常,但应该呈现书籍详细信息的书籍路线给了我一个 404 Not Found 错误。有了这个; 在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。

请有人帮我看看这段代码,告诉我我做错了什么以及如何让它工作。

这是代码。

@app.route("/book/<isbn>", methods=["GET", "POST"])
@loggedin_required
def book(isbn):
    """ Takes and renders user review on the book page."""

    if request.method == "POST":

        # Keep track of individual user per session
        active_user = session["user_id"]
        
        # Fetch data from form
        rating = request.form.get("rating")
        comment = request.form.get("comment")
        
        # Get book_id by ISBN
        row = db.execute("SELECT id FROM books WHERE isbn = :isbn",
                        {"isbn": isbn})

        # Saving id into variable
        _id = row.fetchone() # (id,)
        _Id = _id[0]

        # Check to ensure that its ONLY 1 review/user per book)
        row_check = db.execute("SELECT * FROM reviews WHERE user_id = :user_id AND book_id = :book_id",
                    {"user_id": active_user,
                     "book_id": _id})

        if row_check.rowcount > 0:
            
            flash('You have left a review for this book already.')
            return redirect("/book/" + isbn)

        # Convert to save into DB
        rating_result = int(rating_result)

        db.execute("INSERT INTO reviews (user_id, book_id, comments, rating_result) VALUES \
                    (:user_id, :book_id, :comments, :rating_result)",
                    {"user_id": active_user, 
                    "book_id": _id, 
                    "comments": comment, 
                    "rating_result": rating})

       
        db.commit()

        flash('Review recieved. Thank you!')

        return redirect("/book/" + isbn)
    
    # Take the book ISBN and redirect to his page (GET)
    else:

        row = db.execute("SELECT isbn, title, author, year FROM books WHERE \
                        isbn = :isbn",
                        {"isbn": isbn})

        book_details = row.fetchall()
        
        return render_template("/book.html", isbn=isbn, book_details=book_details)        

这是搜索方法:

@app.route("/search")
@loggedin_required
def search():
    
    """ check if book id is supply"""
    if not request.args.get("book"):
        return render_template("error.html", message="Sorry! you didn't supply a book")
    
    query = "%" + request.args.get("book") + "%"
    
    query = query.title()
    rows = db.execute("SELECT isbn, title, author FROM books WHERE \
                        isbn LIKE :query OR \
                        title LIKE :query OR \
                        author LIKE :query LIMIT 20", 
                        {"query":query})
    
    if rows.rowcount == 0:
        return render_template("error.html", message="No book matches your search")
    
    books = rows.fetchall()
    
    return render_template("search.html", books=books)

这是由搜索功能处理和呈现的 html 页面:

{% extends "layout.html" %}
{% block title %}
search
{% endblock %}
{% block content %}

<div>
    
        {% for book in books %}
        <a href="{{url_for('book', isbn=isbn)}}" class="card-link">
                 {{book.title}} by {{book.author}} 
                </a>
                
        {% endfor %}
            
</div>

herf 链接应该是通过 book route 处理的,但是在搜索后单击链接时,它会通过 404 Not Found 错误。

标签: pythonpostgresqlflaskhttp-status-code-404

解决方案


在你的模板中,我想你应该book.isbn作为bookurl 而不是的参数传递isbn,因为你正在循环books并且它没有在其他地方定义,所以它将被传递None,因此你会得到 404 Not Found。

[..]
        {% for book in books %}
        <a href="{{ url_for('book', isbn=book.isbn) }}" class="card-link">
          {{book.title}} by {{book.author}} 
        </a>
        {% endfor %}
[..]

推荐阅读