Issue with connecting Mongodb Atlas with Django using Mongoengine

I have created cluster using free tier in Mongodb Atlas and also it connected with my current IP address. When I am running python manage.py runserver it gives me error-ServerSelectionTimeoutError at /.

However if I change the IP address to 0.0.0.0/0 then it gets connected and showing the data in the browser. Kindly suggest me how to get successful connection?

Following are the settings I have added in django application:

.env file:

MONGODB_NAME=db_name
MONGODB_HOST=host
MONGODB_USER=user
MONGODB_PASSWORD=password


Django settings.py:

from dotenv import load_dotenv
import mongoengine, os

load_dotenv()


MONGODB_NAME=quote_plus(os.environ.get('MONGODB_NAME'))
MONGODB_HOST=quote_plus(os.environ.get('MONGODB_HOST'))
MONGODB_USER = quote_plus(os.environ.get('MONGODB_USER'))
MONGODB_PASSWORD = quote_plus(os.environ.get('MONGODB_PASSWORD'))


atlas_uri = f"mongodb+srv://{MONGODB_USER}:{MONGODB_PASSWORD}@{MONGODB_HOST}/{MONGODB_NAME}?retryWrites=true&w=majority&appName=Cluster0"

mongoengine.connect(
    db=MONGODB_NAME,
    host=atlas_uri,
    alias="default",
    tls=True
)
Вернуться на верх