I started a Django project, but I get an error with database settings. The homepage works fine:
It worked!
Congratulations on your first Django-powered page.
Of course, you haven't actually done any work yet. Next, start your first app by
running python manage.py startapp [appname].
You're seeing this message because you have DEBUG = True in your Django settings file
and you haven't configured any URLs. Get to work!
But I get this error is console:
settings.DATABASES is improperly configured. Please supply the ENGINE
value. Check settings documentation for more details.
Request Method: GET Request URL: http://fireidea.net/ Django
Version: 1.6.2 Exception Type: ImproperlyConfigured Exception Value:
settings.DATABASES is improperly configured. Please supply the ENGINE
value. Check settings documentation for more details.
Exception Location:
/home2/minhhien/webapps/django/django/db/backends/dummy/base.py in
complain, line 15 Python Executable: /usr/bin/python Python Version:
2.7.5
...
Database settings:
MANAGERS = ADMINS
DATABASE_ENGINE = 'mysql'
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = 'localhost'
DATABASE_PORT = ''
#DATABASES = {
# 'default': {
# 'ENGINE': '',
# 'NAME': '',
# 'HOST': 'localhost',
# 'PORT': '',
# 'USER': '',
# 'PASSWORD': ''
# }
#}
Django 1.6 expects DATABASES to be a dictionary.
You commented it out, and put some older settings format in there.
You should change it back to:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'HOST': 'localhost',
'PORT': '',
'USER': '',
'PASSWORD': ''
}
}
See more about this here: https://docs.djangoproject.com/en/1.6/ref/databases/
You're using settings variables DATABASES_* which was already deprecated in Django 1.2, 5 years ago now!
You need to use a dictionary, as the default setting might have pre-filled for you:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'HOST': 'localhost',
'PORT': '',
'USER': '<user>',
'PASSWORD': ''
}
}
Related
Following the documentation from Django Postgres DocumentationI added to my settings.py
in settings I set
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'OPTIONS': {
'service': 'my_service',
'passfile': '.my_pgpass',
},
}
}
Adjacent to settings.py I created pg_service.conf with
[my_service]
host=localhost
user=USER
dbname=NAME
port=5432
and adjacent to that I created mypg_pass
localhost:5432:PostgreSQL 14:postgres:LetMeIn
I am pretty sure I followed the documentation but on python manage.py check I got the following error.
connection = Database.connect(**conn_params)
File "C:\Users\oliver\base\correlator2v2\correlator2home\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)django.db.utils.OperationalError: definition of service "my_service" not found
It is not able to find the service, but actually one is not required to use the service at all...
OP can simply
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'NAME', # database name
'USER': 'USER',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Adjacent to settings.py I created pg_service.conf
That is not where the system looks for it, see the docs
"By default, the per-user service file is named ~/.pg_service.conf." Where ~ refers to your home directory, not your current directory (and surely doesn't refer to 'the same directory where settings.py is located', since PostgreSQL doesn't even know what that file is)
I have two databases that my Django application needs access. One is a shared database owned by a separate app with the Django app only having read access. The second is entirely owned by the Django app.
For local development I am ok but I'm not sure how to configure things so that Heroku uses the second database.
Currently I have the shared database promoted to DATABASE_URL and the secondary database is at HEROKU_POSTGRESQL_BLUE_URL.
In my settings I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Please ask any more questions if you need me to clarify.
Thanks!
In summary, my specific problem is: I don't know how to have Heroku use the HEROKU_POSTGRESQL_BLUE_URL as the "secondary" database.
---Edit----
At the bottom of settings.py:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())
This is where the connection is made between my app's default database and Heroku's DATABASE_URL. I still haven't solved the issue but after some troubleshooting help in the comments, I believe the answer will be found in there.
Here is my working solution.
I have a local_settings.py file not tracked by version control. This contains the database settings for the developer's postgres instance.
local_settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Then in settings.py I try to import the local_settings.py file. If it doesn't exist, I use the django_heroku package to configure everything for Heroku. In my case, I need to pass the keyword argument databases=False since I want to do a more custom configuration for the databases.
settings.py (only the relevant parts)
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
# These database settings are used for heroku deployments. They are overwritten with local_settings.py in development.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DATABASE_URL'),
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('SECONDARY_DATABASE_URL'),
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
try:
from local_settings import *
except ImportError as e:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals(), databases=False)
In order for this to work you would need to set the Heroku env variable SECONDARY_DATABASE_NAME to the url of your database.
To find out the URL of your secondary database run heroku config -a your_app_name. Copy the url and then set a new environment param in heroku by running
heroku config:set SECONDARY_DATABASE_URL=postgres://xxxxxxx -a your_app_Name
There are a lot of choices with how you would handle the ENV variables, I liked this because I can just set it in my staging and production environments and the same settings work for both.
I have two settings files for my django project with different database settings.
First:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': 'user',
'PASSWORD': 'megahardbreakingpassword',
'HOST': 'localhost',
'PORT': '',
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB',
}
}
}
And second:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite3',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
When I run server with second database settings project works fine, but when I run first settings file with mysql, django returns 404 error for all urls.
I also use django-hosts for implementing subdomains patterns, so maybe something wrong here. My hosts.py code:
from django_hosts import patterns, host
host_patterns = patterns('',
host(r'^$', 'project.urls', name='host'),
)
I use django 1.4.2 and Python 2.7.9
And finally I found answer! According to e4c5's comment I searched problem in my views instead of my settings files. I wrote simple unittest and it gives me some strange tracebeck about my MySQL database such as wrong column name and deprecation warnings about duplication indexes.
I reinstall older version of MySQL server (5.0.70 instead of 5.6.25), run syncdb and migrations again and it completely solved my problems. So God bless TDD!
Settings for database are:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dataBase', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'dataBase',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
and exception generates :
django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.db.backends.sqlite3' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: No module named sqlite3.base
Following django documentations https://docs.djangoproject.com/en/1.7/ref/contrib/gis/tutorial/
change the conf. like this if you are using sqlite (by the this is just for reference made changes according to your need like dbname and etc.)
DATABASES = {
'default': {
'NAME': os.path.join(BASE_DIR, 'yourdbname.sqlite'),
'ENGINE': 'django.db.backends.sqlite3',
}
}
this will create db by name "yourdbname" where your apps settings.py resides plus you can refer to this link i guess it will help you
If you use sqlite3 to store the data, you should change the conf file,here is an example , just change the db file location:
DATABASES = {
'default': {
'ENGINE': 'sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': './db.db', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'dataBase',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Attempting to follow the instructions here to set up a Django instance on Heroku.
Got as far as the installation of Celery, up to the following step:
$ python manage.py syncdb
when I get the following error:
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.
I believe that I have my settings.py file in the right place (project-name/project-name), and I'm running django 1.4.3, but when I try to run manage.py diffsettings, I get the following output:
BROKER_BACKEND = 'django' ### DATABASES = {'default': {'ENGINE':
'django.db.backends.dummy', 'TEST_MIRROR': None, 'NAME': '',
'TEST_CHARSET': None, 'TIME_ZONE': 'UTC', 'TEST_COLLATION': None,
'PORT': '', 'HOST': '', 'USER': '', 'TEST_NAME': None, 'PASSWORD': '',
'OPTIONS': {}}}
Absolutely no idea where the django.db.backends.dummy entry comes from, my settings.py has 'ENGINE': 'django.db.backends.postgresql_psycopg2', which I assume is the correct entry even though the Heroku instructions don't tell you to update it at any point.
Any thoughts what I need to edit here?
I ran into the same issue. In the Heroku docs at https://devcenter.heroku.com/articles/django#prerequisites, it says to add the following to settings.py:
DATABASES['default'] = dj_database_url.config()
You can pass in a parameter of:
DATABASES['default'] = dj_database_url.config(default='postgres://user:pass#localhost/dbname')
And that will allow you to develop locally and on Heroku. The part that actually SOLVES the problem I had though was that the Heroku config environment variable of DATABASE_URL was not actually set. To set this, I ran
$ heroku config
I saw the Database URL assigned to a separate config variable. So I created a new variable:
$ heroko config:add DATABASE_URL={#the database url}
That solved my problem. I hope it helps anyone else with similar issues.
Try add these lines after your DATABASE setting in your settings.py
# Your Database setting.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
# Add these two lines.
import dj_database_url
DATABASES['default'] = dj_database_url.config(default='sqlite://db/sqlite3.db')
Thank you very much Chatri as you suggested adding the default='sqlite://db/sqlite3.db' fixed the issue.