简体   繁体   English

MongoDB - Python Flask 问题:AttributeError: 'Cursor' 对象没有属性 'title'

[英]MongoDB - Python Flask problem: AttributeError: 'Cursor' object has no attribute 'title'

im doing a simple Flask-MongoDB CRUD application but i keep getting the error message AttributeError: 'Cursor' object has no attribute 'title' for my show route.我正在做一个简单的 Flask-MongoDB CRUD 应用程序,但我不断收到错误消息 AttributeError: 'Cursor' object has no attribute 'title' for my show route。

db = mongo.db
products_collection = db.products


@app.route("/product/<product_title>")
def product(product_title):
    product =  products_collection.find({"title": product_title})
    return render_template('product.html', title=product.title, product=product)

Where in my DB the products have a title field.在我的数据库中,产品有一个标题字段。

I belive that the problem is in the "product.title" where it isn't acessing the title through the product variable我相信问题出在“product.title”中,它没有通过产品变量访问标题

You need to define an iteration on product to get a list of documents from collection.您需要定义产品的迭代以从集合中获取文档列表。

@app.route("/product/<product_title>")
def product(product_title):
    products =  products_collection.find({"title": product_title})
    result = []
    for i in products :
        result.append(i)
    p = result[0] 
    #since result is a list you need to specify index, pay attention to this part, if more 
    #than one document is retrieved from collection others will be ignored.
    return render_template('product.html', title=p.title, product=p)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - AttributeError: 'NoneType' object 没有属性 'cursor' python flask - Python - AttributeError: 'NoneType' object has no attribute 'cursor' python flask Python AttributeError:“ str”对象没有属性“ cursor” - Python AttributeError: 'str' object has no attribute 'cursor' Python - AttributeError:&#39;NoneType&#39;对象没有属性&#39;cursor&#39; - Python - AttributeError: 'NoneType' object has no attribute 'cursor' Python MongoDB:“ Cursor”对象没有属性“ name” - Python MongoDB : 'Cursor' object has no attribute 'name' AttributeError: 类型对象“PyMongo”没有带有 Flask Blueprint Mongodb 的属性“db” - AttributeError: type object 'PyMongo' has no attribute 'db' with Flask Blueprint Mongodb python对象AttributeError:类型对象'Track'没有属性'title' - python object AttributeError: type object 'Track' has no attribute 'title' Python Django Shell AttributeError:“文章”对象没有属性“标题” - Python Django Shell AttributeError: 'Article' object has no attribute 'title' Python代码AttributeError问题:&#39;NoneType&#39;对象没有属性&#39;title&#39; - Issue with Python code AttributeError: 'NoneType' object has no attribute 'title' Python Flask:AttributeError:&#39;NoneType&#39;对象没有属性&#39;is_active&#39; - Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active' Python Flask: AttributeError: 'NoneType' object 没有属性 'count' - Python Flask: AttributeError: 'NoneType' object has no attribute 'count'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM