Previous Page Next Page

Configuring the Database

After you have verified that you can start and stop the development server, it is time to configure access to the database. This section takes you through the process of creating and configuring access to the database that will be used in the sample project.

By the Way

Django can dynamically serve web pages without using a database to store information. However, one of the best aspects of Django is its ability to implement database-backed websites.


Configuring the database involves three major steps. The first is to create the database and assign rights. The second is to modify the settings.py file to specify the database type, name, location, and access credentials. The third step is to synchronize the Django project with the database to create the initial tables necessary for the Django engine.

Django supports several different types of database engines. The project used in this book uses a MySQL database. This section assumes that you have already installed, configured, and started a database engine and that it is accessible from the development server.

Watch Out!

The MySQL database does not allow you to use case sensitive names when creating tables. If you want to define objects in your project that have uppercase characters, then you will need to turn off case sensitivity in the Django framework by using the following setting in the <django installation path>/django/db/backends/__init__.py file:

uses_case_insensitive_names = True


Try It Yourself: Create the Database and Grant Rights

This section takes you through the steps of creating the database, creating an admin user, and granting rights from your SQL database command console. You will also modify the uses_case_insensitive_names setting in the Django framework so that you can name objects with uppercase characters. This step will be critical for some of the other Try it Yourself sections.

1.
From your SQL database command console, enter the following command to create a database called iFriends:

CREATE DATABASE iFriendsDB;

2.
Enter the following command to begin using the iFriends database:

USE iFriendsDB;

3.
Enter the following command to create an administrative user named dAdmin with a password called test:

CREATE USER 'dAdmin'@'localhost' IDENTIFIED BY 'test';

4.
Enter the following command to grant all rights on the iFriends database to the dAdmin user:

GRANT ALL ON *.* TO 'dAdmin'@'localhost';

By the Way

If your database engine has a graphical interface that allows you to manage databases and users, you can use that interface as well to create the database and admin user and to assign rights.

5.
Open the <django installation path>/django/db/backends/__init__.py file in an editor.

6.
Add the following setting to the file to disable case sensitivity for the MySQL database:

uses_case_insensitive_names = True

7.
Save the __init__.py file.


Configuring Database Access in settings.py

After the database has been created and a user account set up for Django, you need to configure the settings.py file in your Django project to access that database. Each Django project has its own settings.py file. The settings.py file is a Python script that configures various project settings.

Django uses the following settings in the settings.py file to control access to the database:

Try It Yourself: Configure Django to Access the iFriends Database

The following section takes you through the steps to modify the settings in the settings.py file for the database and user created in the preceding section (a MySQL database named iFriendsDB, and a username of dAdmin with a password of test running on the localhost and default port). Open the iFriends\settings.py file in a text editor.

1.
Find the DATABASE_ENGINE setting, and change the value to the following:

DATABASE_ENGINE = 'mysql'

By the Way

If you have elected to use an SQL database other than MySQL, you need to use that database type here instead of mysql.

2.
Change the value of the DATABASE_NAME setting to the following:

DATABASE_NAME = 'iFriendsDB'

3.
Change the value of the DATABASE_USER setting to the following:

DATABASE_USER = 'dAdmin'

4.
Change the value of the DATABASE_PASSWORD setting to the following:

DATABASE_PASSWORD = 'test'

5.
Verify that the DATABASE_HOST and DATABASE_PORT settings have no value:

DATABASE_HOST = ''
DATABASE_PORT = ''

By the Way

When the DATABASE_HOST and DATABASE_PORT settings are left blank, they default to the localhost and default port. If the database is on a remote server or is running on a nondefault port, these options need to be set accordingly.


Synchronizing the Project to the Database

After you have configured access to the database in the settings.py file, you can synchronize your project to the database. Django's synchronization process creates the tables necessary in the database to support your project.

The tables are created based on what applications are specified in the INSTALLED_APPS setting of the settings.py file. The following are the default settings already specified in the INSTALLED_APPS setting:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
)

The following list describes the default applications that get installed in the Django project:

Try It Yourself: Synchronize the iFriends Project to the iFriends Database

This section guides you through the steps to synchronize the Django project to the database. During the process, Django creates the default tables and prompts you to input the name, email address, and password for a website administration account. The username and password you specify allow you to access the Django authentication system.

1.
Make certain that the development server has stopped by pressing Ctrl+Break from the console prompt.

2.
Change to the root directory of the iFriends project.

3.
Enter the following command at the console prompt to begin the synchronization, as shown in Figure 2.3:

python manage.py syncdb



Figure 2.3. Synchronizing the initial Django project with the database from a command line.


4.
At the prompt, enter a username for the website administrator's account.

5.
At the prompt, enter a password for the website administrator's account.

The database now has the appropriate tables configured to allow Django to use its authentication, content, session, and site frameworks correctly.


Previous Page Next Page