Monday, 24 August 2015

Day 11 - Data Base 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.
    }
}
Here’s a rundown of each setting.
  • ENGINE tells Django which database engine to use. If you’re using a database with Django, ENGINE must be set to one of the strings shown in Table 5-1.
    Table 5-1. Database Engine Settings
    Setting
    Database
    Required Adapter
    django.db.backends.postgresql_psycopg2
    PostgreSQL
    django.db.backends.mysql
    MySQL
    django.db.backends.sqlite3
    SQLite
    No adapter needed.
    django.db.backends.oracle
    Oracle
    Note that for whichever database back-end you use, you’ll need to download and install the appropriate database adapter. Each one is available for free on the Web; just follow the links in the “Required Adapter” column in Table 5-1. If you’re on Linux, your distribution’s package-management system might offer convenient packages. (Look for packages called python-postgresql or python-psycopg, for example.)
    Example:
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
  • NAME tells Django the name of your database. For example:
    'NAME': 'mydb',
    If you’re using SQLite, specify the full filesystem path to the database file on your filesystem. For example:
    'NAME': '/home/django/mydata.db',
    As for where you put that SQLite database, we’re using the /home/django directory in this example, but you should pick a directory that works best for you.
  • USER tells Django which username to use when connecting to your database. For example: If you’re using SQLite, leave this blank.
  • PASSWORD tells Django which password to use when connecting to your database. If you’re using SQLite or have an empty password, leave this blank.
  • HOST tells Django which host to use when connecting to your database. If your database is on the same computer as your Django installation (i.e., localhost), leave this blank. If you’re using SQLite, leave this blank.
    MySQL is a special case here. If this value starts with a forward slash ('/') and you’re using MySQL, MySQL will connect via a Unix socket to the specified socket, for example:
    'HOST': '/var/run/mysql',
  • PORT tells Django which port to use when connecting to your database. If you’re using SQLite, leave this blank. Otherwise, if you leave this blank, the underlying database adapter will use whichever port is default for your given database server. In most cases, the default port is fine, so you can leave this blank.
Once you’ve entered those settings and saved settings.py, it’s a good idea to test your configuration. To do this, run python manage.py shell as in the last chapter, from within the mysite project directory. (As we pointed out last chapter manage.py shell is a way to run the Python interpreter with the correct Django settings activated. This is necessary in our case, because Django needs to know which settings file to use in order to get your database connection information.)
In the shell, type these commands to test your database configuration:
>>> from django.db import connection
>>> cursor = connection.cursor()
If nothing happens, then your database is configured properly. Otherwise, check the error message for clues about what’s wrong. Table 5-2 shows some common errors.
Table 5-2. Database Configuration Error Messages
Error MessageSolution
You haven’t set the ENGINE setting yet.Set the ENGINE setting to something other than an empty string. Valid values are in Table 5-1.
Environment variable DJANGO_SETTINGS_MODULE is undefined.Run the command python manage.py shellrather than python.
Error loading _____ module: No module named _____.You haven’t installed the appropriate database-specific adapter (e.g., psycopg or MySQLdb). Adapters are not bundled with Django, so it’s your responsibility to download and install them on your own.
_____ isn’t an available database backend.Set your ENGINE setting to one of the valid engine settings described previously. Perhaps you made a typo?
database _____ does not existChange the NAME setting to point to a database that exists, or execute the appropriateCREATE DATABASE statement in order to create it.
role _____ does not existChange the USER setting to point to a user that exists, or create the user in your database.
could not connect to serverMake sure HOST and PORT are set correctly, and make sure the database server is running.

No comments:

Post a Comment