Hello world application using Django framework(Python)

In my previous post I have written about the django installation in windows platform and run django development server. In this post I will go through the steps about how to create ‘hello world’  application using python web development framework i.e  django . If you have problem about django installation and creating project in django then please refer to this post .

Here I will assume that you have successfully installed django and created project  named let say ‘mysite’. Now I will go with the step by step process about creating hello world application .

Go to command prompt and navigate to the  django  project folder and type “manage.py runserver” to start the server for that particular project. By default, the runserver command starts the development server on port 8000, listening only for local connections. If you want to change the server’s port, pass it as a command-line argument:

python manage.py runserver 8080

Now we have server running at port 8000 unless any port is defined.

With Django the contents of the page are produced by a view function, and the URL is specified in a URLconf. First, let’s write our “Hello world” view function in ‘views.py’ that we have inside the project folder.

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

In the above code we have imported the HttpResponse class from the django.http class. Then we have defined the hello function which returns the http respose “hello world” . It is required to pass the view parameter as ‘request’ though it has no any use.

Url Configuration File.
In the project folder we can  see the url.py file where we define the patter of the url and its respective view .  Basically, it’s a mapping between URLs and the view functions that should be called for those URLs . For example, “When somebody visits the URL /prabeen/, call the view function prabeen_view(), which lives in the Python module views.py”. By default url.py file will look like:

from django.conf.urls.defaults import *
 
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
 
urlpatterns = patterns('',
    # Example:
    # (r'^mysite/', include('mysite.foo.urls')),
 
    # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
    # Uncomment the next line to enable the admin:
    # (r'^admin/', include(admin.site.urls)),
)

The first line imports all objects from the django.conf.urls.defaults module, which is Django’s URLconf infrastructure. This includes a function called patterns where we define the url patters for our view module. Now lets define the url patterns for the hello function of view module. Before this we have to import the hello view from the view function . Now our code goes like below for the url.py file.

from django.conf.urls.defaults import *
 
from mysite.views import hello
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
 
urlpatterns = patterns('',
# Example:
# (r'^mysite/', include('mysite.foo.urls')),
 
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
# Uncomment the next line to enable the admin:
# (r'^admin/', include(admin.site.urls)),
)
urlpatterns = patterns('',
('^hello/$', hello)
 
)

Here we have imported  the hello view form the view module that we have in project folder. And at the bottom we have defined the url pattern for the hello view  i.e ‘^hello/#’ to make sure that our hello view executes if  ‘hello/’ is found in the url . In the same manner we can add the url patterns for other view function . Django URLconfs allow arbitrary regexes for powerful URL matching, you’ll probably only use a few regex symbols in practice.

Now its time to bring our work into action. Go to browser and type “http://localhost:8000/hello/” , you will see  ‘hello world’ printed in the browser. In the following way we can map the url with the other view functions in the URL confs.

2,032 views
Django, Python

Comments

One Response to “Hello world application using Django framework(Python)”

Leave Comment

(required)

(required)