所以基本上我有一个" / add"路由,它用于手动将测试用户数据播种到mongodb,我希望能够将该用户的ID传递给路由的URL#34; / agreement"因此,当用户在协议页面上时,他们的"_id"
将映射到该URI,他们在协议页面上输入的信息将更新我们在" / add"中添加的数据。路线。
@app.route('/add')
def add():
users = mongo.db.users
users.insert({
"_id" : "autogenID",
"_comment" : " File structure for files in client collection",
"client_name" : "Name_of_firm",
"contact_name" : "Name_of_contact",
"contact_email" : "Email_ID_of_contact",
"contact_password" : "passowrd_encryped_bcrypt",
"client_industry" : "client_industry",
"monthly_checks_processed": "monthly_checks_processed",
"parent_id" : "client_id",
"child_id" : [{
"child_id": "client_id",
"child_id": "client_id"
}],
"agreement_authorised" : "boolean_yes_no",
"agreement" : "agreement_text",
"client_card" : [{
"name_on_card": "client_name",
"credit_card_number": "credit_card_number_bycrypt",
"expiration_date" : "expiration_date_bycrypt",
"credit_card_cvc" : "credit_card_cvc_bycrypt",
"active" : "boolean_yes_no"
}]
})
all_users = users.find()
for user in all_users:
print user
@app.route('/agreement', methods=['GET', 'POST'])
def agreenment(id):
user = mongo.db.users.find("_id")
print user
return render_template('agreement.html', user=user)
我认为问题在于协议资源,也许我应该写/agreement/<id>
,但我认为我错过了对<id>
实例化的基本理解。我也不完全理解协议参数的功能是什么,但我放了(id)
,因为这似乎是为了让用户的信息传递给另一个资源而必须做的事情。
我还认为user = mongo.db.users.find("_id")
可能不正确return render_template('agreement.html', user=user)
。我的一部分认为也许我应该重定向而不是渲染,但如果有人可以伸出援助之手我会非常感激。
答案 0 :(得分:0)
试试这个:
@app.route('/agreement/<user_id>', methods=['GET', 'POST'])
def agreement(user_id):
user = mongo.db.users.find_one({"_id": user_id})
print user
return render_template('agreement.html', user=user)
Flask中的URL参数名称在route
参数中指定,并且必须与装饰函数的签名匹配。
通过传递指定约束的dict来完成对Mongo的查询,因此db.users.find_one({"_id": user_id})
将返回单个结果,其中_id与user_id变量匹配。
编辑:实际上,根据the documentation,您可以这样做:
db.users.find_one(user_id)
。