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 Windows 7 ", we would like to guide you in the creation of an application in this blog.
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!!!!
Your respective django projects should look exactly as depicted in the following table , after the creation of the django application.
Now , lets move ahead to create our first django application.
Python27
|
jango
|
testproject
|
books
|
testproject
|
manage.py
To do the same , run the following command in your command prompts
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
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 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.
use the following link to download it and install the same :
Your sqlite browser will look the same as below and one can open the database created (here, test.db) above to view its contents.
How to create the database tables for Django application
Previously we have discussed about the creation of database in Django, using default sqlite3, now we would like to continue with creation of database tables for Django application named books whose creation has been explained above.
- Open models.py of the django application named books.
- Add the following to models.py to get access to Model Fields from 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 application, lets create a table named Books , which might require fields such as Book name, Book id, author name and time at which it was bought . The table can be created by adding the following piece of code in models.py of books application:
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)
author = models.CharField(max_length = 30)
Following this, move to settings.py under testproject :
Python27
|
jango
|
testproject
|
books
|
testproject
|
settings.py
|
manage.py
|
test.db
To add books application under installed apps.
INSTALLED_APPS = (
'books',
)
'books',
)
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