I managed to get djongo working in my local computer but when I push it to production, I need to set up an admin account for mongo so that my DB doesnt get hacked (again, sigh). Ive searched for a solution for a couple of days, without success.
This is currently the code I have, but its not working:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'db_name',
'HOST': 'localhost',
'USERNAME': 'username',
'PASSWORD': 'password',
}
}
1) How can I configure djongo to access a DB with a username/password? I am only getting errors telling me that it wasnt able to log in to mongo.
2) Ive read a bit about "mongoengine", would you recommend I use that instead of djongo? Why?
Use this format for the Database Configuration
DATABASES = {
'default': {
"ENGINE": "djongo",
"CLIENT": {
"host": "mongodb+srv://<user_name>:<password>#<host_url>/?retryWrites=true&w=majority",
"username": "<user_name>",
"password": "<password>",
"name": "<db_name>",
"authMechanism": "SCRAM-SHA-1", #Add this line if you are using Mongo Atlas Cloud DB
},
}
}
Related
I am super beginner, but want to learn super fast building web application.
I am right now developing an Income-Expense web app on Django Framework (Python, js and Ajax).
I am now stuck with the server and get different errors. Anyone can support me ?
ERROR
"django.db.utils.OperationalError: connection to server on socket "/tmp/.s.PGSQL.5432" failed: fe_sendauth: no password supplied"
I think I shut down everything not properly and when a came back my virtual environment was not working.
Thank You
Don't know what to try more
To resolve the issue, make sure that you have the correct credentials to access your PostgreSQL database. You may need to update the database settings in your Django settings.py file to include the correct database name, username, password, host, and port information.
for example
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
I have an assignment in which I need to set up a database server using Python Django and PostgreSQL. I need to assign the project in Github, and the grader will use my repository to check my project.
In my setting.py file I have the following lines:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'CourseDB',
'USER': 'postgres',
'PASSWORD': '123',
'HOST': 'localhost',
'PORT': '5432'
}
}
What to do so the details on my file will be correct for the grader's side?
Will they have to create a database with the given name, user and password like the ones in my file?
I think that maybe for the database name, I can add in the readme to run CREATE DATABASE CourseDB first. But then again, I don't know their user and password on their machine, So I don't know what should be written in my file in order for my code to work on their machine.
I followed this tutorial on YouTube to create my file.
Unless you need some Postgres specific functionality, you can use the default SQLite backend:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Django will automatically create a database file. No setup/user/password needed.
In general, a good practice is to save all the DB sensitive credentials in settings.py using env vars like the following:
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['DATABASE_NAME'],
'USER': os.environ['DATABASE_USER'],
'PASSWORD': os.environ['DATABASE_PASSWORD'],
'HOST': os.environ['DATABASE_HOST'],
'PORT': os.environ['DATABASE_PORT'],
}
}
Several DB backends are natively supported (see the official docs here) including PostgreSQL, MariaDB, MySQL, Oracle and SQLite and a number of additional backends provided by third parties.
If you go with the SQLite backend (for sure the best alternative for quick development and MVPs), keep in mind that there are some differences specific to the SQLite backend that you should take into consideration (e.g. not supported features).
I am trying to connect a remote Neo4j database to a Django app with a REST API.
I am trying to specify the database in the settings.py file using the following code:
DATABASES = {
'default': {
'NAME': 'papers.db',
'ENGINE': 'django.db.backends.sqlite3',
'USER': '',
'PASSWORD': '',
'PORT': '',
},
}
I would like to know:
What python libraries need to be installed in order to do this?
What is the 'ENGINE' that needs to be specified in the code above?
The Neo4j database has a URI, but does not provide us with an IP address - am I able to use this URI?
I am confident that I know what the other parameters need to be.
Thanks in advance
I'm deploying a django project using zappa to aws-lambda and using mongodb atlas as my database.
I'm tring to connect to the database using djongo.
I set my django_setting in the zappa_settings.json to my project's django settings.
The connection to the database with this settings works just fine in localhost. when deploying, it fails to connect to the server and I suspect that it tries to connect to a default local db (the db sent to mongo_client.py isnt valid or something and it needs to connect to default HOST).
The actual error I get is:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
The above exception was the direct cause of the following exception:
djongo.sql2mongo.SQLDecodeError: FAILED SQL: SELECT
If anyone has an idea I'd would love to hear.
attaching the settings with some fields unset (but set at my settings)
Django settings (database part):
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'db',
'HOST': 'mongodb://<username>:<password>#<>
'USER': 'username',
'PASSWORD': 'password',
}
}
zappa_settings:
{
"dev":
{
"aws_region": "eu-west-1",
"django_settings": settings,
"profile_name": "default",
"project_name": name,
"runtime": "python3.6",
"s3_bucket": bucket,
"timeout_seconds": 900,
"manage_roles": false,
"role_name": name,
"role_arn": arn,
"slim_handler": true
}
}
Try this
'default': {
'ENGINE': 'djongo',
'CLIENT': {
'host': 'mongodb+srv://url',
'username': '<username>',
'password': '<password>',
'name':'<db_name>'
}
}
I'm trying to connect Django rest-api with mongo database which i created on mlab.com. Below is my code which I define in settings.py file in my Django rest-api.
MONGODB_DATABASES = {
'default': {
'NAME': 'dummy',
'HOST': os.environ.get('MONGO_HOST',
'mongodb://dummyuser:dummypassword#ds125851.mlab.com:25851/dummy'),
}
}
mongoengine.connection(
db='dummy',
host=os.environ.get('MONGO_HOST',
'mongodb://dummyuser:dummypassword#ds125851.mlab.com:25851/dummy'),
)
When I run this api I got this error
host=os.environ.get('MONGO_HOST', 'mongodb://dummyuser:dummypassword#ds125851.mlab.com
:25851/dummy'),
typeError: 'module' object is not callable
I tried to search for solutions online but I found examples which were for older versions. I'm using Djangorestframework2.0.7, MongoDB3.4 and mongoengine0.15. I couldn't find any answer for this versions. I tried to connect this api to the local database and I got same error. How can I solve it?
I have been successfully connected django rest-api with mongodb. Here is the solution that works for me.
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'dummy',
'HOST': 'localhost',
}
}
MONGODB_DATABASES = {
'db': 'dummy',
'host': 'localhost',
'port': 27017,
}
Here is the link for more information.
http://blog.tomjohnhall.com/python-3-6-django-2-0-and-mongodb-3-4-3-6/
You can try these steps to connect your django 2.0 or more with MongoDB database:
1) Install mongoengine for django 2.0
pip install -e git+https://github.com/MongoEngine/django-mongoengine.git#egg=django-mongoengine
2)Add these in your settings file:
from mongoengine import *
'django_mongoengine', // Add this line to installed app
MONGODB_DATABASES = {
"default": {
"name": '<db_name>',
"host": 'localhost',
"password": '',
"username": '',
"tz_aware": True, # if you using timezones in django (USE_TZ = True)
},
}
You can find the details for querying the database here