我正在尝试从book_title中检索title属性,但我得到的是<built-in method title of str object at 0x7fb554f9f238>
。我已经将book_title作为参数传递给了路由book,并在booktitle.html中分配了相应的book_title值。
这是我的路由:
@app.route('/search/<title>/',methods=['GET','POST'])
def btitle(title):
book_title = db.execute("SELECT title,author,isbn from books WHERE (title LIKE :title)",params={"title":title}).fetchall()
if request.method == 'GET':
book_title = db.execute("SELECT title,author,isbn from books WHERE (title LIKE :title)",params={"title":title}).fetchall()
if book_title:
return render_template("booktitle.html",book_title=book_title)
else:
return render_template("error.html")
else:
book_title = db.execute("SELECT title,author,isbn from books WHERE (title LIKE :title)",params={"title":title}).fetchall()
if book_title:
return redirect(url_for("book",book_title=book_title))
@app.route('/books/<book_title>/',methods=['GET','POST'])
def book(book_title):
if request.method == 'GET':
return render_template("individualbook.html",book_title=book_title)
这些是 booktitle.html
和 individualbook.html
的HTML页面。
{% extends "layout.html" %}
{% block title %}
{{ book }}
{% endblock %}
{% block body %}
<h1>Search results</h1>
<ul>
{% for book in book_title %}
<li>
<a href="{{ url_for('book', book_title=book_title) }}">
{{ book.title }}
</a>
</li>
{% endfor %}
</ul>
{% endblock %}
individualbook.html
{% extends "layout.html" %}
{% block title %}
Book
{% endblock %}
{% block body %}
<h1>Book Details</h1>
<ul>
<li>Title: {{ book_title.title }}</li>
<li>author: {{ book_title.author }}</li>
<li>isbn: {{ book_title.isbn }}</li>
</ul>
{% endblock %}
当我尝试获取
title
、author
和isbn
的值时,我得到了Title:<built-in method title of str object at 0x7fb554f9f238>
,而author
和isbn
的值为空。
db.execute()
返回的东西只是一个元组列表,它不是一个带有字段属性的对象。title
只是字符串类上的一个方法名称。 - Daniel Roseman