MongoDB-Python烧瓶问题:AttributeError:';光标';对象没有属性';标题';

MongoDB-Python烧瓶问题:AttributeError:';光标';对象没有属性';标题';,python,mongodb,flask,Python,Mongodb,Flask,我正在做一个简单的Flask MongoDB CRUD应用程序,但我一直收到错误消息 AttributeError:“Cursor”对象没有显示路线的属性“title” db=mongo.db products\u collection=db.products @应用程序路线(“/product/”) def产品(产品名称): product=products\u collection.find({“title”:product\u title}) 返回呈现模板('product.html',t

我正在做一个简单的Flask MongoDB CRUD应用程序,但我一直收到错误消息 AttributeError:“Cursor”对象没有显示路线的属性“title”

db=mongo.db
products\u collection=db.products
@应用程序路线(“/product/”)
def产品(产品名称):
product=products\u collection.find({“title”:product\u title})
返回呈现模板('product.html',title=product.title,product=product)
在my DB中,产品有一个标题字段


我相信问题出在“product.title”中,它没有通过产品变量访问标题

您需要在产品上定义一个迭代,以从集合中获取文档列表

@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)

@app.route(“/product/”)
def产品(产品名称):
products=products\u collection.find({“title”:product\u title})
结果=[]
对于产品中的i:
结果.追加(i)
p=结果[0]
#由于结果是一个需要指定索引的列表,所以请注意这一部分(如果更多)
#从集合中检索到多个文档,其他文档将被忽略。
返回呈现模板('product.html',title=p.title,product=p)

您需要在产品上定义一个迭代,以从集合中获取文档列表

@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)

@app.route(“/product/”)
def产品(产品名称):
products=products\u collection.find({“title”:product\u title})
结果=[]
对于产品中的i:
结果.追加(i)
p=结果[0]
#由于结果是一个需要指定索引的列表,所以请注意这一部分(如果更多)
#从集合中检索到多个文档,其他文档将被忽略。
返回呈现模板('product.html',title=p.title,product=p)

我找到了解决方案!我换了一个,找到了一个和这个产品。产品的标题,它成功了!我找到了解决办法!我换了一个,找到了一个和这个产品。产品的标题,它成功了!