Insert data into with pymongo and flask

Neeraj Sharma picture Neeraj Sharma · Jul 19, 2015 · Viewed 7.3k times · Source

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>

Answer

Blakes Seven picture Blakes Seven · Jul 19, 2015

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.