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
- 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 # admin.autodiscover() urlpatterns = patterns('', # Examples: # 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)), url(r'hello/$',myfunction), )
my views.py(In hello/views.py)
# Create your views here. from django.http import HttpResponse def myfunction(request,): return HttpResponse("Hello")
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 !!!
13 thoughts on “Hello World in Django !!!!”
Great blog here! Also your web site loads up very fast!
What web host are you using? Can I get your affiliate link
to your host? I wish my web site loaded up as quickly
as yours lol
We like to think of it as a premium that pays offf handsomely ‘ and certainly it should.
I noticed a change immediately when I suddenly was sleeping through the
night for the first time in years. Remove Issues Delete – Rip Off reports standing supervision stop – Ripoff
report* Private Branding – A current survey by
i – Prospect exhibits that 90% of research engine end users cease their research inside the
very first handful of web pages.
Post looks nice. It would be helpful for beginners. Keep on posting.
Thanks …Ya sure …
Did not work… The Django is running on command line and the browser shows the ngnix welcome page..
on which port django is running? check the port
After you flush, you cross your fingers because lately you’ve been grabbing the plunger quite a bit.
The name Norwalk was derived from Norwalk, Ohio, the place where the virus was first identified in connection with the outbreak of waterborne disease in one of the
town’s primary schools. These cameras give the plumber
a clear picture of the break or obstruction in the sewer line.
Shop for wholesale cheap Ravens jerseys and get our ultra fast 7-day shipping standard and 365 day returns on any size order only at our wholesale cheap Ravens jerseys online shop.
wow thanks ^_^
Looking to hook up?
This is exactly what I needed