Previous Page Next Page

Hour 3. Adding Models and Objects to Your Website

What You'll Learn in This Hour

  • How to install the Django admin interface model application

  • How to configure the URLconf file to allow web browsers to see the admin interface

  • How to activate a model in the admin interface

  • How to add, delete, view, and modify objects using the admin interface

  • How to add fields to data models

  • How to change the behavior of fields in a data model

  • How to create relationships between classes in data models

In Hour 2, "Creating Your First Website," you went through the steps to create a basic Django website. As part of that process, you created a simple application and model. In this hour, you will activate and install Django's admin interface so that you will be able to manage the data in the models from the web. This hour also provides more details on creating and managing models, including some of the different types of fields that you can add to a model and which options are available to control the behavior of those fields.

Installing the Admin Interface Model

Django's admin interface is actually a Django application. Therefore, it must be installed just like any other application you create. Installing Django's admin interface is a simple process of adding the application to the list of installed applications in the settings.py file and then synching the database. This process creates new tables in the database to support the admin interface.

Try It Yourself: Install the Admin Interface as an Application

In this section, you install the admin interface in your iFriends project.

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

2.
Open the iFriends\settings.py file in an editor.

3.
Find the INSTALLED_APPS setting, and add the django.contrib.admin application to it, as shown in the following snippet:

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

4.
Save the file.

5.
Synchronize the Django admin application into the iFriends database by using the following command from the root of the iFriends project:

python manage.py syncdb


By the Way

The first time you run the syncdb utility, you are asked to create a superuser, which is required to access the Django admin site. If you don't create a superuser at that point, you can create one at any time by running the following utility:

djanngo/contrib/auth/bin/create_superuser.py


Previous Page Next Page