Create a hello world django application

Most programmers started from somewhere and that somewhere is termed as hello world application. So here i will walk you through to create a hello world django application, so that you understand what is behind starting a django project and how to create an application in django and also link that application with the main project.

What we are going to do in this article?

This question might arise in someone’s mind that what we are going to build in this tutorial. Let me clear that we are not making something fancy here. Instead we will create a traditional hello world application and trust me you will learn something new here.

Creating Virtual Environment

In order to install django and start creating project in django, the first thing that you will need is to create a virtual environment.

To create a virtual environment, i had written a comprehensive tutorial on this but don’t worry if you don’t want go and read that post, i will explain here.

First open up CMD and install the package called virtualenv into your computer.

What virtualenv will do is it will enable you to create a virtual environment into your PC.

To install virtualenv, type the following command.

pip install virtualenv

The above command will install virtual environment into your PC.

To create a virtual environment, just open up cmd and type the following command.

virtualenv env

Now let’s activate your virtualenv so that you can install django inside your virtual environment.

To activate your virtual environment, type the following command in command prompt.

env\Scripts\activate

for Linux or mac operating systems, you need to type the following command.

source env/bin/activate

This will activate your virtual environment.

Installing django

Now that you’ve successfully installed virtual environment package and activated that virtual environment. It is time to install django in our virtual environment.

To install django, simply type the below command in CMD.

pip install django

It will take a little bit of time and your django package will successfully installed.

Creating a django project

For creating a django project, just type the below command.

django-admin startproject hello_world_project

This will create a new project into your virtual environment, Now it is time to run and check weather our django project is working or not?

To run your django project, navigate to the project root folder and type the below command in command prompt.

python manage.py runserver

It will start your server and you can check that your project is working or not by typing http://127.0.0.1:8000/ and you will see something like this.

Create a hello world django application

Now that you project is successfully running, go ahead and start making our hello_world application.

What applications does in django is that we create an application for the different purposes. i.e one might for contact page, one might be for blog page and then we integrate those applications with the main project and this keeps our project easy to carry and easy to share applications with our colleges or team members.

Creating hello world application in django

To create application head over to CMD and type the below command.

python manage.py startapp hello_world

This will create a new folder inside your project and you will have some other files inside this.

Let’s break our application folder structure one by one.

You will have a models.py inside your hello_world application, which can be used to create database tables and store data inside them by creating different models.

Next, you will have an admin.py file inside your application and this can be used to register our database table and models inside administration section.

Another file will be present there named as views.py which will handle all the views that we will create in this application.

As our application is simple, we will create a simple view but it comes handy to know what each file can do.

Register application inside settings.py file:

Now the next step is to open the projects main settings.py file and register your hello_world application under installed_apps.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello_world',
]

Open up views.py file inside hello_world application and add these lines of code to it.

Creating Views

from django.shortcuts import render
from django.http import HttpResponse


def hello_world_view(request):
	return HttpResponse('Hello World!')

Here we are creating a simple view named hello_world_view and we are just returning HTTP response.

Creating URL patterns.

Create a new file inside hello_world application folder and name it as urls.py and add these lines of code to it.

from django.urls import path
from . import views

urlpatterns = [
	path('', views.hello_world_view, name='hello_world')
]

What we are doing here is simple. We are importing views that we just created, from the current directory and using it with an empty url path so that it shows up on the home page.

Editing projects main URL File.

Now open up urls.py file inside projects root folder which as also termed as projects main URL. Add these lines of code to project’s main urls.py file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('hello_world.urls')),
    path('admin/', admin.site.urls),
]

Running our django project

All of our settings are configured and we have created views and URLs, now we are ready to run our project and hopefully you by typing the command python manage.py runserver, you will see something like this.

Create a hello world django application

Hopefully, you have understand everything about how to create a hello world django application and if you want to check the program structure and how everything works while starting a new project in django then click here. I hope you’ve learned a lot, please leave a feedback below if you have any confusion. Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *