Saturday, 6 January 2018

Writing your first Django app - PART 1


Django is a python based web framework that let's you create webapps quickly and with less code. It's free and opensource. For more details on the framework itself visit -
In this post we will create a sample app in Django python framework.


This tutorial assumes you are using Django 2.0, which supports Python 3.4 and later.

Install python3 and pip3 -
  • sudo apt-get install python3-pip
Next install Django python framework using pip3 -
  • sudo pip3 install Django 
You can see the installed version of python and django in various ways. Some are given in screenshot below -

Creating a Django project

Create a skeleton of your Django app using following command -
  • django-admin startproject djangodemo
You should see a directory getting created with name djangodemo. Inside this you should have file and another directory with same name djangodemo. This inner directory named djangodemo is actually a python package. Outer directory is just a holder with file. file is used to give you command line tasks to interact with your django project. You can see the version of you django framework used with following command -
  •  python3 version 

Directory structure is as follows -

 Some other pointers other than ones mentioned above -
  • tells python this directory should be considered as a package.
  • This also means your inner djangodemo directory is a python package.
  • Your Django app settings go here.
  • URLs used in your Django project go here.
  • This is an entry-point for WSGI-compatible web servers that can serve your project.
 Now that you have created your project let's run it with following command -
  • python3 runserver

Ignore the warnings for now.

NOTE : You don't have to restart the server everytime you make changes to code. Django handles it. Just refresh the pages.

Open -
 You should see installation successful message as follows -

NOTE : By default your server will run on port 8000. But you can change it as follows -
  • python runserver 8080

Creating Django App

A project is collection of apps and it's configurations needed for a website to run. Apps are modules that run in your project. A project can have multiple apps. Similarly a app can be part of multiuple projects. Apps can be at any python paths.

You can create a app as follows -
  • python3 startapp testapp
I like to put all apps in a directory called apps in your actual python package directory. You can do that as follows -

Creating your webpage

Go to your apps directory and edit to add following content -

from django.http import HttpResponse

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

 Next in the same directory create a file called and add following content to it -

from django.urls import path
from . import views

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

Finally go to your project directory  - djangodemo/djangodemo and edit file to have following content -

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

urlpatterns = [
    path('test/', include('djangodemo.apps.testapp.urls')),

Next in apps directory inside djangodemo directory create a file called You can do this using -
  • touch
Now simply run your server and visit -
to see your site.

Understanding : First we created an app called testapp. It should have some default files like stores all your views. Here we added a new view called index and mapped it inside a file to the root url ("") at the app level. Next we mapped this to at our project level for '/test'. include maps the url provided and forwards rest the included module. In this case it will check url has 'test/' and forward the rest which is - "" to the in the testapp where we have mapped request view to "". So request view gets rendered.

NOTE : Note how we added a file in apps directory. This is to ensure python recognizes this directory as a package. So that we could use djangodemo.apps.testapp.urls in the of project.

That's it you created your 1st django project and app. We will see some more details about this in next post. Thanks.

Related Links

No comments:

Post a Comment

t> UA-39527780-1 back to top