为什么我在 0x7fb554f9f238> 处获得 str 对象的<内置方法标题而不是值?



我试图从book_title中检索title属性,但我得到了<built-in method title of str object at 0x7fb554f9f238>。我已经将book_title作为参数传递给路线簿,并在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.htmlindividualbook.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 %}

当我尝试获得titleauthorisbn值时,我得到的Title:<built-in method title of str object at 0x7fb554f9f238>authorisbn值为空。

您刚刚放入|book_title.title|,但它应该是|book_title.title((|。我想当你插入括号时它会起作用。。。

  • 在路由app.btitle中,传递给render_templatebook_title变量被明确分配了数据库响应对象,这些对象可能会正确响应属性解析

  • 在路由app.book(individualbook.html(中,book_title是框架基于请求数据和路由注释设置的函数/路由的自变量。该变量的类型为str,不具有.author.isbn的任何属性。这里的巧合是,python3str有一个title方法(参见pydoc3 str.title(,因此模板中有意外的输出。

最新更新