ElasticBeanstalk Django: Please supply ENGINE value - python

I am trying to install a Django app with ElasticBeanstalk. It's failing with the exception settings.DATABASES is improperly configured. Please supply the ENGINE value
Full error:
Command failed on instance. Return code: 1 Output: (TRUNCATED)... in
complain raise ImproperlyConfigured("settings.DATABASES is improperly
configured. " django.core.exceptions.ImproperlyConfigured:
settings.DATABASES is improperly configured. Please supply the ENGINE value.
Check settings documentation for more details.
container_command 01_migrate in .ebextensions/15_my.config failed. For more
detail, check /var/log/eb-activity.log using console or EB CLI.`
I am getting this error while one of my command is getting executed:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python manage.py migrate --noinput"
leader_only: True
Here's my config/settings/production.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ebdb',
'USER': 'djangousername',
'PASSWORD': 'djangopassword',
'HOST': env('RDS_HOSTNAME'),
'PORT': '5432'
}
}
I am kind of sure that EB is picking the config from this file only and not any other file because if I am changing something in this file, it is reflected while ElasticBeanstalk env creation. I have also searched in my whole application that DATABASES is mentioned only in either this file i.e. <app-rot>/config/settings/production.py or <app-root>/config/settings/local.py. At both the places, I have provided the ENGINE value as above.
Please let me know if I need to add any other relevant information

This took me forever to figure out (as most things with Django and EB). I think the environment variables you set in your console take preference over variables or settings you set anywhere else e.g. .ebextensions.
In my case the error was because my settings file was set correctly in my ebextensions but incorrectly in the console variables. Thanks #daniel for the hint

Related

Django py manage.py makemigrate

What can I make with it? I'm beginner in python and django. I download it and I i wrote py manage.py makemigrate and I've get error. Can u help me?
Your issue is with your DB configuration in the setting.py. If you are using the default SQLite then copy/paste this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
and your project will work just fine. After this, run
python manage.py makemigrations
python manage.py migrate #copy all migrations to the database
python manage.py createsuperuser #to have a admin user to login to adminpanel
python manage.py runserver #starting the server
Otherwise, take a look at the official documentation how to connect MySQL, PostgreSQL, Oracle databases and required configurations.
Your error is in here:
SQLite is not like MySQL or other databases. Actually, it is not a real database. You are using a port, username, password and etc. These are the cause of the error. SQLite is not running in the server or another place. It is just a single file contains data information. Update yours to mine above and it should start work again or change your database to MySQL or others.
You need to supply all environment variables that are listed in your settings file. Such as DB_NAME that presented in your screenshot. Search for os.environ[<VARIABLE_NAME>], every VARIABLE_NAME should be defined.
If you are a beginner it is better to stay with the documentation and do like https://docs.djangoproject.com/en/2.1/intro/tutorial01/
If you could share the DB part of the settings.py it would help.
Generally python manage.py startapp appname should create the necessary files for you.
After which a python manage.py makemigrations and python manage.py migrate should work properly. And this should not come.

ImproperlyConfigured: Requested settings DATABASES

I have seen 10 different ways of configuring the MySQL database with Django, and none of them work.
I am running MySQL 8.0 and
Django 2.0
I updated the Project/settings.py with the database settings as followed:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name_of_db',
'USER': 'root_user',
'PASSWORD': 'not_telling_u_my_pass_:)',
'HOST': 'localhost',
'PORT': '3306',
}
}
When I run the command to test the connectivity:
python manage.py dbshell
I get error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
No other settings have been made, however, I have tried to follow a few guides which say that I must declare:
set DJANGO_SETTINGS_MODULE=mysite.settings
I have also issued:
python manage.py shell
With no luck. I am running Windows 7 as well. I have all of the mysql dependencies installed for connecting from Python to MySQL. I know this question has been asked a number of times, but a thread should exist with clear instructions. Many of the other threads do not have clear and concise instructions, and I'm using a later version of MySQL so hopefully this will help solve others issues experiencing the same.
Project Tree:
Server
|
+-- Server
| |
| +-- _init_.py
| +-- settings.py
| +-- urls.py
| +-- wsgi.py
+-- manage.py
Update 1 (10 mins after posting):
I ran the python manage.py migrate and this installed all of the tables into my database schema. However, when I run the python manage.py dbshell command again, I still get the same error above saying the settings are not configured.
Update 2:
After running:
python manage.py shell --settings=Server.settings
I get:
python manage.py dbshell
CommandError: You appear not to have the 'mysql' program installed or on your path.
The ImproperlyConfigured often masks other errors. As you note, your database configuration is correct, which means the problem is most likely one of two problems.
Some file imported during setup is causing an exception
There is an import in settings.py which is in turn trying to use a setting (i.e., settings is used while being loaded)
First, if you have any imports in your settings.py, make sure those aren't going to in turn import django.conf.settings (imports like import os) are fine.
Next, inspect files from your project that might be imported during setup (apps, urls, custom middleware, template contexts, etc.) for possible errors that might arise during import. Make sure you can import all files on their own (some may complain because they require setup to be run first, you'll just have to inspect those extra carefully, or remove references to settings so that you can test them on their own).
I met the same problem and I have fixed the problem finally. The reason is that MySql will treat different when you using the 'localhost' parameter. It will connect to the local server using a Unix socket file.
Check this document:
https://dev.mysql.com/doc/refman/5.7/en/connecting.html
So you should use '127.0.0.1' instead of 'localhost' in DATABASES.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name_of_db',
'USER': 'root_user',
'PASSWORD': 'not_telling_u_my_pass_:)',
'HOST': '127.0.0.1', #This string will works!
'PORT': '3306',
#If you want to use 'localhost', you should provide the path
# of the UNIX socket. You should check /etc/my.cnf(the mysql
# config file) to get this parameter.
'OPTIONS': {'unix_socket': '/tmp/mysql.sock'}
}
}
That will work in MySql 5.7, I am not test in MySql 8.0. Another suggestion: check user privileges in MySql, may be something wrong.

Unable to config django for mysql

I'm trying to connect Django app to MySQL on windows 10. I have installed mysqlclient as an interface between my Django app and MySQL. Whenever I try to open dbshell I'm getting error something like this:-
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Here is my setting.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog',
'USER':'root',
'PASSWORD':'root',
'HOST':'127.0.0.1',
'PORT':'3306'
}
}
I have tried other solutions on StackOverflow but it didn't work for me.
I also tried deleting the dbsqlite3 file from the project directory.
Versions for different elements are as follows:-
Python (3.6)
Django (1.11.4)
mysqlclient (1.3.12)
pip (9.0.1)
MySQL(5.7)
DJANGO_SETTINGS_MODULE variable is not available. run
python manage.py shell
if you linux user and run
python3 manage.py shell
as you use python 3.6
Finally, I solved this problem. I was using django-admin dbshell command instead of python manage.py dbshell
In case of windows, set env variable for MySQL in user variables section, otherwise you will get CommandError saying that mysql is not installed.
For Ubuntu use python3 manage.py dbshell

Configuring Postgres on Heroku with Django

I am trying to link a Heroku Postgres databse with my Django application. I have created the database and linked it to my Heroku application using this tutorial. I can't seem to get it working with Django, however.
I am able to access the database through the heroku pg:psql command. But when I try to run python manage.py migrate, Django gives...
settings.DATABASES is improperly configured
Please supply the ENGINE value.
In settings.py, I have...
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default=os.getenv('DATABASE_URL'))
}
I have already promoted the correct heroku postgres database to my app's DATABASE_URL variable, which I can verify through heroku config.
You've misread the instructions. It should be:
DATABASES = {
'default': dj_database_url.config()
}
dj_database_url already parses the env var, you don't need to pass it explicitly.
Heroku config variables aren't available in Django settings.py when testing locally.
https://stackoverflow.com/a/21763381/3783608

How to use dj-database-url while connecting with postgresql in heroku using python

I'm here because I'm really really new with heroku-python-django-postgresql group. I have googled for a usage for dj-database-url and I don't understand why i have to use it when developing a python application that needs to connect with postgresql. I have added postgresql (dev version) as add-on to my application, but I don't know how to tell to the app that I want it to use my db.
so, the short question is, How do I indicate to dj-database-url that I want to use my database?
Thanks for your time and answers, I'll appreciate your help because this is very very urgent!
dj-database-url is a utility to help you load your database into your dictionary from the DATABASE_URL environment variable. Heroku uses environment variables for your database and other addons. To begin using your database you'd simply use the below command to setup your DATABASES dictionary:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
And maybe stash DATABASE_URL in your virtualenv activate script.
May be help for param in setting.py
DATABASES = {
"default": dj_database_url.config(
default="postgres://postgres:saleor#localhost:5432/saleor", conn_max_age=600
)
}
"database url" in default string have the value
postgresql://[user[:password]#][netloc][:port][/dbname][?param1=value1&...]
Use pip module dj-dtabase-url. Add an environmental var with the name 'DATABASE_URL' and the value from the heroku db settings.
Important, to avoid the error 'NameError: name 'DATABASES' is not defined.'
You still have to leave for example the default DATABSE settings.
The complete code:
import dj_database_url
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase',
}
}
DATABASES['default'] = dj_database_url.config()

Categories

Resources