Answer the question
In order to leave comments, you need to log in
Python how to connect to a remote Mongo base using flask-mongoengine?
Good evening
I can’t connect to the Mongo base for an hour using the flask-mongoengine library, I also tried pure mongoengine, without success. Answer one
mongoengine.connection.ConnectionFailure: You have not defined a default connection
from flask import render_template
from app import app
from models import UserProduct
@app.route("/")
def index():
products = UserProduct.objects
print(products)
from flask import Flask
from flask_mongoengine import MongoEngine
from config import Configuration
app = Flask(__name__)
app.config.from_object(Configuration)
db = MongoEngine()
class Configuration(object):
DEBUG = True
JSON_AS_ASCII = False
SECRET_KEY = 'secret_key'
MONGODB_SETTINGS = "mongodb+srv://"name":"pass"@cluster0-vkzx6.mongodb.net/"base"?retryWrites=true&w=majority"
from mongoengine import *
class UserProduct(Document):
description = StringField()
order_num = IntField()
link = StringField()
Answer the question
In order to leave comments, you need to log in
Thank you for your help!
Working example
from flask import Flask
from flask_mongoengine import MongoEngine
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = "key"
app.config['JSON_AS_ASCII '] = False
app.config['MONGODB_HOST'] = "mongodb+srv://<user_name>:<pass>@cluster0-vkzx6.mongodb.net/<db_name>?retryWrites=true&w=majority"
db = MongoEngine(app)
from mongoengine import *
class User(Document):
description = StringField()
order_num = IntField()
link = StringField()
@app.route("/")
def index():
user = User.objects
print(user)
if __name__ == "__main__":
app.run()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question