Всем привет
Начал изучат mongodb по видео этого парня
https://www.youtube.com/watch?v=vVx1737auSE
Создал базу данных на
https://mlab.com
конектится с таким кодом он без проблем, логин и пароль верные.
from flask import Flask
from flask_mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['MONGOALCHEMY_DATABASE'] = 'dbname'
app.config['MONGOALCHEMY_CONNECTION_STRING'] = 'mongodb://username:pass@ds231315.mlab.com:31315/bestpricedb'
db = MongoAlchemy(app)
class User(db.Document):
name = db.StringField()
class query(db.Document):
qeryz = db.StringField()
но когда я пытаюсь поднять небольшой сервер:
from flask import Flask
from flask_mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['MONGOALCHEMY_DATABASE'] = 'dbname'
app.config['MONGOALCHEMY_CONNECTION_STRING'] = 'mongodb://username:pass@ds231315.mlab.com:31315/bestpricedb'
mongo = MongoAlchemy(app)
@app.route('/add')
def add():
user = mongo.db.users
user.insert({'name' : 'Lega'})
return ('User add')
if __name__ == '__main__':
app.run(debug=True)
сервер запускается,но при переходе в 'add' выдает вот такую ошибку : builtins.AttributeError
AttributeError: 'MongoAlchemy' object has no attribute 'db'
Traceback (most recent call last)
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\PythonServer\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\PythonServer\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\PythonServer\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\waidones\Envs\bestprice\mongo_connect.py", line 13, in add
user = mongo.db.users
AttributeError: 'MongoAlchemy' object has no attribute 'db'