Hi …After long time i ‘m blogging about django..Lets start our journey to django .Before reading this i would suggest to get some knowledge in python .
My suggestions for python learning are
- A byte of python with pdf
- Dive into Python
- Learn python the Hard way
What is Django?
Django is a Web framework written in python language .
what is meant by frame work?
After searching about framework, Frameworks are similar to libraries or templates that are already written for you, you can re-use the code to build your system . i.e(Collection of codes that uses some control mechanisum).
still didn’t get???
I explain with my view, lets forget about all technical terms and others. Lets say you want to build your new house ?
To build new house what are the things you needed?
- empty ground (place)
- sand and others stuffs etc….
now same thing for building your system(project)
Framework offers everything you needed, the only thing you need to do is ,place the things in correct manner. like(database details in proper place, your logics , representation of your data. etc.)
Hope you understand something !!!!
now coming to our django framework.It follows MVC pattern like Ruby on Rails .
MVC: => Model View Controller
Model: for Database access.(this contains database,table details)
Views: your logic goes here
Templates: To represent your data(html files)
Why MVC or What is the Advantage of this?
The biggest Advantage of MVC is ,one’s change doesn’t affect other.
for example if you want to change your database details(say db name or table details) you need to change in model file only( no need of find and replace in entire project).Because each and everything are loosely coupled(independent). Same thing applies for view and templates too.
didn’t get it??
while doing sample code i will explain it …
To install django see install
Sorry i ‘m not going to tell about installation . Since i’m an linux user I know about installation in linux only. You can find lot of source for your operating system installations.
Django documentation is fair enough for any os.
After installing ,lets create the sample project.
Its good practice to create working directory for learning any language .
create directory mkdir djcodes(here djcodes is my directory name ,you can give any name)
cd djcodes(change to working directory)
To start a new django project (i’m using django 1.5 version):
django-admin startproject sample # here sample is a project name
change directory to sample
now issue ls command ,
You have manage.py file and sample folder inside sample project .
manage.py – points to the settings file in your project. (This files are automatically created by django)
Inside sample folder you have 4 files
__init__py – this will indicate your project as python package to compiler.
urls.py – file to hold the urls of your website .(e.x)http://localhost/hello
here in order to use /hello in our project you have to mention this in urls.py.
(see my below explanation for these concepts)
settings.py – File that will hold all apps,database settings of your information .
(if you open this file means you can see,time zone,templates etc..). You will learn more about these files in my upcoming posts.
Wsgi.py– This file handles our requests and responses .(our django development server)
Ok. Lets start the server by
./manage.py runserver (Note inside project directory)
this is show like this
0 errors found
Django version 1.4.5, using settings ‘sample.settings’
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[17/Jan/2014 11:45:00] “GET / HTTP/1.1” 200 1957
Open your favorite browser and see the link http://127.0.0.1:8000/
Congratulations on your first Django-powered page.
Of course, you haven’t actually done any work yet. Here’s what to do next:
- If you plan to use a database, edit the
DATABASES setting in
- Start your first app by running
python manage.py startapp [appname].
You’re seeing this message because you have
DEBUG = True in your Django settings file and you haven’t configured any URLs. Get to work!
This is default page in django .
Our goal is Hello world page in django .
Lets Start creating our app and display hello world
To create an app(inside project directory)
django-admin startapp hello
This will create a hello folder in project directory.It has four files
__init__.py – this file indicates your app as python package.
models.py – file to hold your database informations
views.py – your functions to hold requests,logics etc.
tests.py – for testing purposes.
Three things to do for our task
1. add url in urls.py with associate function.
2. write code for url in urls.py
3. html file to render a response.
Lets add url in urls.py
add your like this
from django.conf.urls import patterns, include, url
from hello.views import myfunction
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
urlpatterns = patterns('',
# url(r'^$', 'sample.views.home', name='home'),
# url(r'^sample/', include('sample.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
my views.py(In hello/views.py)
# Create your views here.
from django.http import HttpResponse
save your files .
now run server ./manage.py runserver
Now see in browser by typing http://localhost:8000/hello
this will return hello in page . Thats it our goal is done .
What happens behind the scene . I will explain shortly.
when you type localhost:8000/hello —> this will send request to django
this is will read urls.py and look for pattern and url match . if it matches it calls the associated function.
In our case it matches hello in urls.py and calls myfunction in views.py.
In views.py –> we have the code to display the hello as HttpResponse .
Thats it .. Thanks for reading… Happy coding !!!