python - 这里没有做些什么?
问题描述
我已经在这里跟踪了几个星期,尽我所能尝试一切,但没有预期的结果。我想在用户搜索并单击图书链接时显示图书的详细信息。我的搜索路线工作正常,但应该呈现书籍详细信息的书籍路线给了我一个 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 错误。
解决方案
在你的模板中,我想你应该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 %}
[..]
推荐阅读
- database - Mongo 是单个集合还是多个集合?
- python - 如何让 Python ufuncs 返回大于正常浮点大小限制的浮点数?
- google-ai-platform - 如何使用 Python 从 Vertex AI 上的训练模型访问特征重要性的文本数据(值)
- html - 根据文本框的数量展开一个div
- python - 来自 crontab 与控制台的 python 脚本:打印和日志记录的顺序不同
- slack - SLACK-MODALS 自定义休假/权限批准或拒绝自定义应用程序
- vue.js - 无法读取未定义的属性(读取“toLowerCase”)
- python - Endianess - 十六进制到十进制小端。不正确的数据类型
- javascript - 如何自动运行我的滑块以及单击图像以进行下一步并单击按钮以转到该幻灯片功能?
- python - 重组列表/元组结构