Getting Started with Django, Part 1

Django is a Python-based web framework that traces its roots all the way back to 2003. The first public release was in July 2005 and it’s been free and open source ever since. The framework is used by big projects, including Instagram, Clubhouse, and Mozilla.

In this tutorial, you’ll learn about the Django MTV model and project structures, and how to set up your admin credentials. You’ll also learn about Django apps, URL routing, models, forms, templates, and how to render data in templates. In Part 2 of this series, you’ll apply CRUD (Create, Read, Update, and Delete) operations to create a useful Todolist web application.

The Django framework follows a Model-view-controller (MVC) architectural pattern. Let’s introduce that pattern in the following section.

MVC pattern

The Model-view-controller architectural pattern is commonly used for designing web applications. The benefit of this practice is to separate the logic of your app to three elements: model, view, and controller.

As stated by Martin Fowler in his 2003 book Patterns of Enterprise Application Architecture, the controller receives the user request, sends the appropriate message to the model, receives the response from the model, and passes the response to the appropriate view for display.The Django framework adapts this approach with a slight tweak. It uses a Model-template-view (MTV) framework where the user interacts with the database through the model and sends a request. The model returns the request to the appropriate view which passes it to the templates for display.

Django set up

You can find the finished source code for this project in this GitHub repo. If you prefer to follow along, you can set up Django, initiate a virtual environment, and install it with this command:

python3 -m venv venv; . venv/bin/activate; pip install --upgrade pip
pip install django

In this tutorial, we’ve set up Django v4.1.2. However, you can use your own version because it won’t differ much for this basic tutorial.

Todo Django project structure

Now, let’s build a todo project with Django. First, create a new directory called DJANGO_TODO and navigate to it:


Next, build the Django boilerplate:

django-admin startproject todo .

The startproject subcommand with the . will create the todo project which has the Django project structure inside the current working directory (DJANGO_TODO).

So, the tree of this structure (excluding the virtual environment directory) will be:

└── todo

Note: If you omit the dot in the django-admin startproject command, you’ll have two todo’s directories: one at the current directory and the other will be the child directory with the file. To avoid the confusion of the two directories, we used the DJANGO_TODO as the base URL of the todo project and todo as the project directory.

Each file has a purpose:

  • executes specific administrative Django tasks similar to the django-admin command-line utility.
  • contains the asynchronous gateway interface (ASGI) configurations for your Django project. You probably won’t see file if you’re using an earlier Django version. This file is installed by default when starting the Django project. WSGI or ASGI configurations are recommended configurations for deploying your Django project.
  • allows the project to be a Python package that can be imported elsewhere. Note the is a generic Python file used to make Python packages; it’s not Django-specific.
  • contains the configurations of the Django project.
  • contains the URL routings for the Django project.
  • contains the web server gateway interface (WSGI) configurations of your Django project.

Running the web server

To run the Django web server, use the runserver subcommand on the command line:

python runserver

Open your browser and navigate to http://localhost:8000. You’ll see the following starter page:

django installation

If you look at the terminal, you’ll see the following warning:

terminal warning for django setup

This means there aren’t any database migrations already done for this project. Django by default has a SQLite database set up in its configuration settings. So, let’s apply the database migrations:

python migrate

Now, when you run the server again, you’ll see the earlier warning is omitted.

Creating the admin

Django has a set of applications installed by default, such as the admin app. We want to set up the admin credentials because we want to use this admin to enter and retrieve data in our todo project.

You can create the admin username, email, and password with the following:

python createsuperuser

After you enter them and don’t encounter any errors, you’ll see this message: “Superuser created successfully.”Now, run the server back again with python runserver and head over to http://localhost:8000/admin. You’ll see a login prompt:

django admin login prompt

Log in with your registered admin credentials. You’ll then be redirected to the admin page. You can use this page later in this tutorial to create your todo items.

Creating and configuring a Django app

A Django app is used to group a project’s functionalities. For this todo project, we will create an application for the tasks. If you want to extend it further, you can add other apps as you need, like an app for history, another app for user information, and so on.

There are Django apps created by default when starting a Django project, like admin and auth. There are also third-party apps created for specific needs. The way an app is structured makes it easy to configure. Apps are created as subdirectories to the project directory.Let’s see that in action and create the tasks app:

python startapp tasks

Now, the tasks app is created with a subdirectory after the same name. Inside that directory, you’ll see files like the following:

  • which allows Python packaging as in the todo directory.
  • which contains the admin definitions for the app.
  • which contains configuration parameters for the app.
  • which contains the database configurations for the app.
  • which contains test cases for the functionalities of the app.
  • which contains the view functions for the app.

Now, open the todo/ file and app the new app tasks into the INSTALLED_APPS list:


URL routing

To test the new tasks app, you need to route the URLs that you want to access the app from. To configure the content that should be represented, edit the tasks/

from django.http import HttpResponse

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

This index view method returns an HTTP response of a “Hello world!” string. To receive this response, you need to configure what URL this content will be displayed on.

So, create a new file tasks/ for the tasks URLs and fill in the following:

from django.urls import path
from . import views

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

Django looks for the urlpatterns variable to search for the requested URL. This variable is a list of Django paths. At this time, we have one URL, which is the root URL. The path() function takes the path directory, the views method, and the name of that URL. The list name will be used later in this tutorial.

To configure all URLs contained in the tasks app to the whole todo project, include the tasks URLs in the urlpatterns list for the todo project in the todo/ file:

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

urlpatterns = [
    path('', include('tasks.urls'))

Now, open the browser and enter http://localhost:8000. You’ll see the “Hello world!” message on the screen.

Django templates

Templates in Django define the layout of the page format sent to the user after the view method is invoked.

Create a new directory, templates, inside the tasks app and then create another directory inside named after the app (tasks).

In that directory, create a new HTML file called list.html (i.e. this tasks/templates/tasks/list.html path):

<h1>To Do</h1>

To display this h1 heading to the user, you need to configure the view method to render that template. Open the tasks/ file and edit it to the following:

from django.shortcuts import render

def index(request):
    return render(request, 'tasks/list.html')

The render function is a Django shortcut to render the Django templates. In this case, the function takes the request sent by the user and the template path (tasks/list.html).

To view the template on the browser, open the localhost:8000. You’ll see the To Do h1 heading is displayed.

Django models

To set up a database in Django, you need to create models in the file. We will use SQLite, the default database engine set up by Django. We will interact with it through Django Object-Relational Mapper (ORM) which deals with objects instead of native SQL queries.

Open the tasks/ and enter the Task class as follows:

from django.db import models

class Task(models.Model):
    text = models.CharField(max_length=200)
    is_done = models.BooleanField(default=False)
    created_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.text

The Task model has three attributes:

  1. text, a string field to represent the text of the todo item. The length can’t be 200 characters.
  2. is_done, a boolean field to show whether the todo is done or not. Its default value is False.
  3. created_date, a date and time field. The auto_now_add option, here, automatically sets the creation date to now when the todo item is created.

Right now, Django doesn’t know about the models in this app. To make the migrations, use the makemigrations command:

python makemigrations

When you run that command, Django searches for any change in the files for all Django apps listed in the INSTALLED_APPS list. When Django finds a new file, it creates a new migration file for the app like this:

django migration file

To apply the recently added migration file to the database, use the migrate command:

python migrate

To do operations on this Task model, you need to configure the model in the tasks/ file:

from django.contrib import admin
from .models import Task

Go to the admin page on your browser:

django admin page

As you can see, the Task model has been already set up. If you click on the “Add” button, you’ll see the following fields:

django admin dashboard

Add your todo items in the text box of the text attribute.

The following items are added to that model:

add tasks to todo list

Rendering data in templates

To render data in templates, you need to pass your desired objects from views to templates. You can edit the index view method in the tasks/ as follows:

from django.shortcuts import render
from .models import Task

def index(request):
    tasks = Task.objects.all()
    context = {'tasks': tasks}
    return render(request, 'tasks/list.html', context)

The tasks instance here is a QuerySet in Django, which is iterable. When you iterate over it, you’ll get all the entries of tasks stored in the database.

To display all the todo items in the root URL, you need to edit the list template in the tasks/templates/tasks/list.html path:

<h1>To Do</h1>

{% for task in tasks %}
{% endfor %}

Open the localhost:8000 and you’ll see the bullet points of your items:

Django todo list app in action

Learn more about using Django

Now that you’ve got the foundations in place, are you ready to extend your Django project? Check out Getting Started with Django, Part 2 to learn how to create and use forms for CRUD operations.

Read more about:


Ezz is an AWS Certified Machine Learning Specialist and a Data Platform Engineer. Check out his website for more.