2

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>

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't know why OP getting error but even deprecated .insert() still works in pymongo 3.0.3 which the current version
@user3100115 You're right "deprecated" would be better wording here. The message was intenedd to say "use the new methods" in short.
@NeerajSharma undefined objects are a sure sign of errors. make sure your connections are occuring wthout error. And please make sure the code you are actually using is "scoped" the same was as shown here. Right now collection should be globally scoped and therefore available. You error suggests it is not.
1

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

2 Comments

After doing like this code is running successfully ... but no data is getting inserted into db
@NeerajSharma how are you checking that?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.