[英]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.