2016-06-11 6 views
1

基本的に私はmongodbに手動でテストユーザーデータを書き込むための "/ add"ルートを持っており、そのユーザーIDをURLに渡すことができるようにしたいユーザーが契約ページにアクセスしたときにはそのルートに「/ agreement」と入力すると、"_id"がそのURIにマップされ、契約ページに入力した情報が「/ add」ルートでシードしたデータを更新します。Python-FlaskとmongoDBでユーザーデータを渡す

@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)と同様に正しくない可能性があります。私の一部は、レンダリングの代わりにリダイレクトを行うべきかもしれないと考えていますが、誰かが手を貸すことができれば、私はそれを高く評価します。フラスコ内の

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

URLのパラメータ名はrouteパラメータで指定されている、と彼らは装飾された関数のシグネチャと一致する必要があります。

+0

[組み込み関数](https://docs.python.org/3/library/functions.html#id)をシャドーイングするので、識別子として 'id'を使用しないでください。 – dirn

答えて

0

はこれを試してみてください。

Mongoでのクエリは、制約を指定するdictを渡すことによって行われます。したがって、db.users.find_one({"_id": user_id})は、useridの変数に一致する_idで単一の結果を返します。

編集:実際には、the documentationごとに、次のようにすることができます。 db.users.find_one(user_id)

関連する問題