How To Create Your First Django Web Application

Posted by in Articles, Programming, Python

Here is step by step tutorial that will guide you to create Django Web application . Django is Python framework, that allows you to build high-performing, elegant Web applications quickly.

What you’ll need?

  • Python version 2.6.5 or higher
  • Django framework

How To install Python 2.7.5 ?

Go to python.org/getit , choose suitable version for your operatying system and download it . I have windows 7 64 bit, so I downloaded “Python 2.7.5 Windows X86-64 Installer (Windows AMD64 / Intel 64 / X86-64 binary [1] — does not include source)”

install python

Follow the usual steps for installation of software and install python to your pc.

After insalling python , I recommend you to set up Environmental Variables to be able to run python from Command prompt in every directory .

To set up Environmental Variables in windows 8 , in the Search field type System variable , under settings you’ll see option “Edit the system environment variables” , click it

system environmentv ariables

under System Variables , in variable Path add C:\Python27\.

path variable

Run Command prompt, and type python , you should be able to run python , as shown on image below

python cmd

How To Install Django ?

Download Django framework from djangoproject.com/download/ , and extract Django-1.5.2.tar.gaz to your PC . For example I extract Django to directory C:\Users\blaz1988\Documents\Django-1.4.6

Run Command Prompt as Administrator and use CD command to navigate to directory, where is extracted Django

For example :

cd C:\Users\blaz1988\Documents\Django-1.4.6

cmdtt

To install Djanog type

python setup.py install

After this Django will be installed under Python directory, under site-packages.

To start programming Django web application, you need to create Django project. Navigate to directory where is Django instaled,
for example

cd C:\Python27\Lib\site-packages\django\bin

Then run command for creating Django project

django-admin.py startproject project_name

For exmaple I’ve created project called “mojaaplikacia” ,
project django

To run Django default server, navigate to your project directory

cd C:\Python27\Lib\site-packages\django\bin\mojaaplikacija

and run command

manage.py runserver

runserver

Launch your web browser and type http://127.0.0.1:8000/ , if you see ” It worked” as shown on image below, then you have successfully installed Django.

project django

it worked

How To Create “Hello World” program in Django ?

Go to your Django project directory, there you’ll see python file urls.py By default urls.py has code , modify that code to :

from django.conf.urls import patterns, include, url
from views import hello
urlpatterns = patterns(",
url(r'^mojaaplikacija/', hello),
)

The above code calls the view “hello”, when someone type in web browser URL www.yourdomainname/mojaaplikacija , if you use localhost , it will be http://127.0.0.1:8000/mojaaplikacija/ .
After changing the file urls.py, you need to create views.py, where will be defined views as hello.

In views.py type following code :

from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, world. ")

The above code receive request and return HTTP response with text “Hello World” .