So open up the DOS window and go to "Django Projects" and then to your project folder "mysite". Here, enter the following command to create the new application, let's call it 'contacts'.
python manage.py startapp contacts
If the command is executed successfully, you will get the DOS prompt again.
Now use 'dir' command to check whether the 'contacts' folder has been created or not. And if you look into this folder, you will find the following files created:
admin.py - For enabling the application(s) to appear in the Admin web site
models.py - For creating different models or table data associated with the application
tests.py - For creating test cases to test the application
views.py - For creating views or presentation layer to display the related data
__init__.py
As the name of the application suggests, we are going to create a Contacts list.
For storing a contact, we need things like Name (First and Last), Phone Number, Email Address, Date of Birth, Street Address, City, Zip Code etc. These things can be declared or specified in the 'models.py' which will direct the django framework to create database tables and fields accordingly.
So using the IDLE editor, open up the 'models.py' file and enter the code as shown below:
Each model is declared as a class here which inherits the 'models' class part of 'django.db'. So each model is created as a separate table in the database with its properties as fields/ columns. In this case, django creates the new table 'Contact' in the database with its columns like 'first_name', 'last_name' etc.
Notice that each field is declared as like 'CharField', 'EmailField' or 'DateTimeField'. These are the instances of the 'Field' class that comes with django. Based on how the field is declared, django decides about what datatype it should use while creating the database table and also decides how these should be displayed in the Admin web site along with the necessary validations. We will discuss about the different types of fields django provides in another post.
django does not automatically create the database tables and fields whenever you make changes to the 'models.py' file. At first, you need to add your application name in the 'settings.py' file we discussed in our earlier post in the INSTALLED_APPS property:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'contacts',
)
And then type in the following command to create the database tables and fields:
python manage.py syncdb
Note that, whenever we use the 'syncdb' command, it will lookup the 'settings.py' file and under the INSTALLED_APPS property to check whether any new changes were made to this and updates the database accordingly.
If you are curious to know how the sql script would look like or how django creates the database table and fields, you can execute the following command:
python manage.py sql contacts
It should looks like the following screen:
If you are little more curious to look at the database and the tables/ fields created you can download the 'sqlite database browser' application which is available here.
There are few ways in which you can start adding data for the 'Contact' model created above. One way is to include this in the Admin web site where the django framework takes care of creating the appropriate forms for adding, editing, deleting or listing the contact details for you automatically. Other way is to use the Python command shell and add, edit, delete or list the contact details. Or we can create each of the forms individually and present them using html files.
We shall discuss about the first option now. In order for your application models to appear in the Admin web site, you need to register them using the 'admin.py' file present in the 'contacts' folder as mentioned above.
So, open up the 'admin.py' file using the IDLE editor and enter the code shown here:
The first line is included automatically when this file is created. It tells django to import the 'admin' class from the 'django.contrib' namespace.
Second line tells django to import our 'Contact' model from the 'contacts' application. If I forgot to mention earlier in our posts, the hash or pound symbol (#) denotes Python's way of specifying the comments. So, any text after the hash or pound symbol is ignored by Python compiler.
The last line tells django to register the 'Contact' model in the Admin web site. Now, let us fire up the development server by running the following command:
python manage.py runserver
And go to http://127.0.0.1:8000/admin to access the Admin web site.
When prompted with the Login screen, enter the user credentials you have created at the time of creating the database for the first time. If everything goes well, you should be seeing this screen:
Note how 'Contacts' section was added here by django framework. You can now add, edit, delete or view the contacts. Notice the 'Add Contact' form here and observer how django designs the form and fields based on how the model was created.
django takes care of validation too without writing any additional code. Again, based on how the model was created, validation rules are applied to the relevant fields. For example, if you click on 'Save' button at the right side of the screen, you will see the validation errors displayed like below:
Also, since we declare the 'Email' field as type 'EmailField()', django implements the relevant validation by checking whether the value entered for this field is a valid email address or not. Sounds amazing, right?
Keep playing around in the Admin web site and check how easily we can do many things. We shall discuss how to use the Python command shell to do the same things with Contacts.