Neeraj Sharma
Neeraj Sharma

Reputation: 71

Insert data into with pymongo and flask

when i click on submit button i get an error which says:

"TypeError: 'Collection' object is not callable. If you meant to call the 'insert' method on a 'Database' object it is failing because no such method exists."

here is my signin.py code :

from flask import Flask, request, render_template
from pymongo import MongoClient

@app = Flask(__name__)
connection = MongoClient()
db = connection.project #database name.
collection = connection.signup # collection name.

@app.route('/signin/')
def index_show():
     return render_template('signin.html')

@app.route('/signin/', methods = ['POST'])
def signup_form():
   username = request.form['user']
   passowrd = request.form['pass']
   collection.insert({'user': username, 'passoword': passowrd})

if __name__ == '__main__':
   app.run(debug = True)

my html file code is here :

  <!DOCTYPE html>
 <html>
  <head>
    <title></title>
  </head>
  <body>
     <form method="post" action=".">
       <input type="text" name="user" /><br/><br/>
       <input type="password" name="pass" /><br/><br/>
       <input type="submit" name="submit" /><br/><br/>
     </form>
 </body>

Upvotes: 2

Views: 8220

Answers (3)

Shahul Es
Shahul Es

Reputation: 106

please do make sure that you do this to an existing database,object has been returned successfully. here is my code:

from pymongo import MongoClient

client=MongoClient()
db=client.testdb
new={"shopclues":1234,"rating":3}
result=db.testdb.insert_one(new)   
result.inserted_id
ObjectId('59e2f0f2031b4b0b27ecfa09')

Upvotes: 0

bereal
bereal

Reputation: 34312

I think, the root cause is in this line:

collection = connection.signup  # collection name.

Contrary to the comment, you're getting a DB named signup. That should rather be:

collection = db.signup

Upvotes: 1

Blakes Seven
Blakes Seven

Reputation: 50426

The method has been deprecated and is changed to .insert_one() in the pymongo 3.x driver, there is also .insert_many() for multiple document creation:

collection.insert_one({'user': username, 'passoword': passowrd})

The .insert() method is now only supported in the 2.x and lower series.

Upvotes: 5

Related Questions