Whether you are programming for first time or you are a professional programmer, learning new programming language means you will have fun and difficult time. You may come up with various courses and tutorials over the internet or books but writing your first program is painful. We don’t know where to start and what to do for the first time. So I came up with this post part of special project series to help you to create a very simple django app and guide you through all the steps for your first program or app experience in django programming platform.

Preparing Environment

  • Installing Python

    1. The first step to create your django app is to install python. To install python visit https://www.python.org/downloads/. Download the latest version and install it.
    2. After installing python check and verify whether it is installed succefully or not by running below command in Command Prompt or Terminal window.
      python --version
      
  • Creating directory

    1. In this step we will create a folder for our project. Go to Command prompt and type
      mkdir tutorials
      cd tutorials
      
  • Creating Virtual Environment

    In order to seperate our project dependencies from other projects we will create a virtual environment. To create a virtual environment steps are given as:

    1. Install virtualenv library for python
      pip install virtualenv
      
    2. Create virtual environment
      virtualenv env
      
    3. Activate virtual environment
      env\Scripts\activate
      
  • Install Django

    Once you have activated virtual environment now its time to install Django.

    1. To install django type following command
      pip install django
      

Creating Project

  • Create your First Project for Django

    Before creating apps in django we need to create the django project. A django project may consist of several apps.

  1. Type following command to create new django project in the current directory
    django-admin startproject my_project .
    

Here my_project is the name of your django project. You can name it as you wish.

  • Test the project

    Once you have completed above steps now its time to test our django project.

  1. To launch the server type following command
    python manage.py runserver
    
  2. To test our project open browser and enter this url http://127.0.0.1:8000
    If you see something like below then our project is working.

django-success

Creating our First App

Now its time to create our django app.

  • Create app

    1. To create django app type following command
      django-admin startapp helloworld
      

      Here helloworld is the name of our django app. You can name it as you wish.
      Our project directory structure will look like below image django-directory-structure

    2. After creating the app we need to connect our app to django project.
      To connect our app to django project, open the settings.py file under my_project folder.
      And add helloworld under INSTALLED_APPS section. It should be something like below image.

django-installed_apps

It’s time to code

After setting up our project and app, now we need to create our view and connect it to the target url.

Creating View

View is where we will write backend code for our app.

  1. Open views.py file under helloworld folder
  2. Add below code to this file and save it.
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
	return HttpResponse('Hello World')

In the above code, index() is our view which takes the request and in return it gives us HttpResponse(). The HttpResponse has our message Hello World which will be printed to the webpage. The view index() will be triggered when we call target url.

Adding target url for our view

  1. Open urls.py under my_project folder
  2. If you open the urls.py file you will see something like below
from django.contrib import admin
from django.urls import path

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

Now you need to import your view that is index
And provide it a url like below code.

from django.contrib import admin
from django.urls import path
from helloworld.views import index

urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', index),
]

Here we have added two lines of code
First is from helloworld.views import index This will import our view so that we can access it to provide a url.
Second is path('hello/', index), This one will provide a url to our view index

Running our App

Its time to see our output.

  1. To launch the server type following command
    python manage.py runserver
    
  2. To test our app open browser and enter this url http://127.0.0.1:8000/hello/

If you will open above url. Then you will see the following page. django-hello_world_app
That’s it, our first project on django is completed.

The source code for this project is hosted in my github repo. Please feel free to use and have queries.