为什么我得到 <built-in method title of str object at 0x7fb554f9f238> 而不是 value ?

Sha*_*Sid 5 python jinja2 flask

我试图从 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)
Run Code Online (Sandbox Code Playgroud)

而且,这些是用于booktitle.html和的 html 页面individualbook.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 %}
Run Code Online (Sandbox Code Playgroud)

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 %}
Run Code Online (Sandbox Code Playgroud)

当我试图让titleauthorisbn值我得到Title:<built-in method title of str object at 0x7fb554f9f238>authorisbn值是空的。

Rad*_*odó 0

  • 在路由中,传递给的变量app.btitle被显式分配数据库响应对象,该对象可能正确响应属性解析book_titlerender_template

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