Search⌘ K
AI Features

Hello World Application

Understand the process of creating a Django application inside a project. Learn to add the application to settings, write a Hello World view using HttpResponse, and map the view to a URL. Gain the skills to run the Django development server and see the output in the browser.

In the previous lesson, we saw how to create a Django project. In this lesson, we will take a look at how we can create a Django application. In Django terms, the project and application are very different. A project is comprised of multiple different applications.

What is a Django application?

A Django application is created to perform a particular function for a Django project. For example, a project could have a registration application, a comments application, a polling application, etc.

Pluggable Django applications

These Django applications can be plugged into other Django projects, so we can reuse them or use other people’s applications in our project.

The following command will create a Django application in the first_project directory:

python manage.py startapp first_app 

In this example, startapp is the keyword to create the application and first_app is the name of the application that we are creating.

The following files will be included in the first_app directory:

These files are:

  • migrations/: This directory stores database specific information as it relates to the models.

  • __init__.py: This is a blank Python script that, due to its special name, lets Python know that this directory can be treated as a package.

  • admin.py : This is a Python script where we can register our model classes. We will discuss it further in later chapters.

  • apps.py : This is a Python script where we can place application-specific configurations.

  • models.py : This is where we store the application’s data models. We specify the entities and relationships between the data.

  • tests.py : Here we store the test functions to test our code.

  • views.py: This is where we have functions that handle requests and return responses.

Running the application

Before we run our first_app application that will show us Hello World!, we need to perform the following steps:

Step 1: Add the first_app application

We need to tell Django that we created this application. In order to do this, we need to go to the settings.py file of our project and add the application to the list of installed applications. Open the settings.py file, scroll down, and we will see a bunch of variables. The one that we are looking for is called INSTALLED_APPS. This variable contains the list of applications that Django adds by default, including authorization, administration, messages apps, etc. Let’s add in our first_app application as a string and save the file.

Step 2: Add a Hello World view

We need to create a view in our application which shows Hello World! For that we open first_app/views.py file.

First we have to import an HttpResponse object from the django.http module. For this, add the following line:

from django.http import HttpResponse

Then, we create a view function called index. Each view takes at least one argument—conventionally, we call it a request, but we can call it whatever we want. Each view must return an HttpResponse object. This response object takes a string parameter representing the content of the page. Since we just want to return Hello World!, we add the following lines:

def index(request):
    return HttpResponse("Hello World!")

NOTE: Each view of this application is going to exist within the views.py file as its own individual function.

Python
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World!")

NOTE: This HttpResponse can also take a string of HTML. So, we can return something like <h1>Welcome to Educative<h1>, too.

Step 3: Map a view to a URL

In order to see this view, we have to map it to a URL. Open the first_project/urls.py file. We will see a list of function calls to URLs.

First, we have to import views from first_app:

from first_app import views

Then, we add the following URL in urlpatterns list:

path('',views.index, name='index'),

As we can see, the first parameter is the path of the URL.

NOTE: The path means that the given string will be appended to the IP address or DNS name of the server. For example, if the path is 'home', then the URL will be host_name/home. In this case, where we have provided the path '', it will be matched when we don’t enter anything after the DNS name or IP address of the server.

The second parameter is the name of a function to which we have mapped this URL, and the third parameter is the name of this specific URL.

Python 3.5
from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
path('',views.index,name="index"),
path('admin/', admin.site.urls),
]

Execute the example

Now that everything is set, we just need to use the following command to run the server:

python manage.py runserver

For your convenience, we have set things up so that when you click the RUN button below, the command will automatically be executed and you will see the output of the “Hello World!” example.