Looking for django Answers? Try Ask4KnowledgeBase
Looking for django Keywords? Try Ask4Keywords

Django A complete hello world example.


Example

Step 1 If you already have Django installed, you can skip this step.

pip install Django

Step 2 Create a new project

django-admin startproject hello

That will create a folder named hello which will contain the following files:

hello/
├── hello/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

Step 3 Inside the hello module (the folder containing the __init.py__) create a file called views.py:

hello/
├── hello/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py  <- here
│   └── wsgi.py
└── manage.py

and put in the following content:

from django.http import HttpResponse

def hello(request):
    return HttpResponse('Hello, World')

This is called a view function.

Step 4 Edit hello/urls.py as follows:

from django.conf.urls import url
from django.contrib import admin
from hello import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.hello)
]

which links the view function hello() to a URL.

Step 5 Start the server.

python manage.py runserver

Step 6

Browse to http://localhost:8000/ in a browser and you will see:

Hello, World