Total Pageviews

Thursday, 3 July 2014

Creation of Application in Django Python

Continuing with the creation of a project in django from our previous blog on “ How to install Django on Ubuntu/Windows 7 “, we would like to guide you in  the creation of an application in this blog.


Now that you have created your project named “testproject” , the following table can help you sync with  the skeleton of the python/django-kruthika/testproject/  folder (if on ubuntu) or Python27/jango/testproject  folder  (if on windows 7) present on your system:


Ubuntu
Windows 7
python
       |
 django-kruthika
                |
                testproject
                           |
                           testproject
                           |
                           manage.py

[testproject and manage.py are present in testproject]
Python27
       |
      jango
                |
                testproject
                           |
                           testproject
                           |
                           manage.py

[testproject and manage.py are present in testproject]


If your django project folders sync with the above respective skeletons , good job you are on the right path :)


Now , lets move ahead to create our first django application.Note here that , the commands to create  django application is same on both operating systems i.e., Ubuntu & Windows 7.


Your respective django projects should look exactly as depicted in the following table , after the creation of the django application :


Ubuntu
Windows
python
       |
 django-kruthika
                |
                testproject
                           |
                           books
                           |
                           testproject
                           |
                           manage.py   

Python27
       |
     jango
                |
                testproject
                           |
                           books
                           |
                           testproject
                           |
                           manage.py
To do the same , run the following command in your command prompts(in windows) or in terminals(in ubuntu):
                                       
                                          python manage.py startapp books      


You will be able to see that , the newly created django application “books” has been created under the django project (named as testproject  here).   



 Default contents of Django application on creation     
   On creation of django application , certain files are created as a part of the django application. These are  :
  •    views.py
  •     models.py
  •     tests.py
  •     admin.py
  •     __init__.py


                          Skeleton of the entire Django Project

d.png

   


You will be able to understand from the above mapping that the entire django project will revolve around files named views.py , models.py , and the templates. urls.py file ,which is under the testproject folder of the django project, connect the views.py and the templates.


                                                         Creation of Database
Database is very integral part of any application  so hence , lets go ahead and create
a database for our application.
If you already have had a quick tour of the contents of the django project , you would have noticed a file named setting.py residing under testproject folder of your django project named testproject.
In settings.py , you will be able to find DATABASES.


                                               NAME of DATABASES in ubuntu
DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.sqlite3',
       'NAME': '/home/kruthika/Documents/python/django-kruthika/testproject/test.db',
   }
}


                                               NAME of DATABASES in windows
DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.sqlite3',
       'NAME': 'C:/Python27/jango/testproject/test.db',
   }
}


Your default database engine is sqlite3 and we suggest you to go ahead with the same instead of meddling with mysql or any other engine.


For the database name , kindly give the entire path of the database you want it to reside in and end the path with the name of your database.


Now create the database just named above, using the following command :
                                                        python manage.py syncdb


On running the above command , you will be able to see the file test.db having been created under the django project(here testproject).


To view the contents of the database created :
To view the contents of the database which we will talk about it later , you will have to install the sqlite browser for the same.


For Ubuntu :
To get sqlite browser for ubuntu , search for sqlite database browser in ubuntu software centre and install the same.


For windows :
use the following link to download it and install the same :
                                     http://sqlitebrowser.org/
Your sqlite browser will look the same as below and one can open the database created (here, test.db) above to view its contents.
db.png





How to create the database tables for Django application


In our previous blog, we wrote about the creation of database in Django using default  sqlite3, in this blog we would like to continue with creation of database tables for Django application named books whose creation has been explained in the previous blog.


  1. Open models.py  of the django application named books.
  2. Add the following to get access to Model Fields under  django.db
                              from django.db import models
                             from django.db.models import Model


Refer : https://docs.djangoproject.com/en/dev/ref/models/fields/ to choose the model fields required for your application. Here in books, lets create a table Books and add few fields :


                              class Books(models.Model) :
                                       name = models.CharField(max_length = 30)
                                        id = models.CharField(max_length=30)
                                        time_at_which_it_was_bought = models.DateTimeField(auto_now_add=True)

The first two fields are CharField,and the third field (DateTimeField) is to store timestamp,where auto_now_add is for storing the present time,which is made true.
Following this, move to settings.py under testproject :

Ubuntu
Windows
python
       |
 django-kruthika
                |
                testproject
                           |
                           books
                           |
                           testproject
                                  |
                              settings.py
                           |
                           manage.py
                           |
                           test.db   

Python27
       |
     jango
                |
                testproject
                           |
                           books
                           |
                          testproject
                                  |
                              settings.py
                           |
                           manage.py
                           |
                           test.db  

To add books application under installed apps.
              INSTALLED_APPS = (
   'books',
  )

Add this in your settings.py if not present to get the current time set,
In Windows TIME_ZONE = 'UTC+05:30'
In Ubuntu  TIME_ZONE = 'Asia/Kolkata'

Move to terminal , to validate the creation of tables
                                           python manage.py validate


We just wrote about the design of the required database tables in models.py. To run the code and create the tables type the following on the terminal,
                                              python manage.py syncdb




No comments:

Post a Comment