这里有什么没做的?
What haven't done right here?
我已经在这里跟踪了几个星期,尽我所能尝试一切,但没有预期的结果。我想在用户搜索并单击图书 link 时显示图书的详细信息。我的搜索路径工作正常,但本应呈现书籍详细信息的书籍路径却给我一个 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>
herflink本来应该是书本路径处理的,但是搜索后点击link,却出现404 Not Found错误。
在您的模板中,我猜您应该将 book.isbn
作为 book
url 的参数传递,而不是 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 %}
[..]
我已经在这里跟踪了几个星期,尽我所能尝试一切,但没有预期的结果。我想在用户搜索并单击图书 link 时显示图书的详细信息。我的搜索路径工作正常,但本应呈现书籍详细信息的书籍路径却给我一个 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>
herflink本来应该是书本路径处理的,但是搜索后点击link,却出现404 Not Found错误。
在您的模板中,我猜您应该将 book.isbn
作为 book
url 的参数传递,而不是 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 %}
[..]