【问题标题】:What haven't done right here?这里没有做些什么?
【发布时间】:2020-10-31 16:57:23
【问题描述】:

我已经在这里跟踪了几个星期,尽我所能尝试一切,但没有预期的结果。我想在用户搜索并单击图书链接时显示图书的详细信息。我的搜索路线工作正常,但应该呈现书籍详细信息的书籍路线给了我一个 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链接应该是通过书本路线处理的,但是搜索后点击链接,却出现404 Not Found错误。

【问题讨论】:

  • 添加用户点击问题链接时使用的 URL。
  • 如果这是没有运行的代码,那么更改非常好,您的问题在其他地方。你能告诉我们搜索方法的路由定义和签名吗?

标签: python postgresql flask http-status-code-404


【解决方案1】:

在您的模板中,我想您应该将book.isbn 作为book url 的参数而不是isbn,因为您在books 上循环并且它没有在其他地方定义,因此它将作为@987654325 传递@ ,因此您将得到 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 %}
[..]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-28
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多