How do you set up an Activity application backend in Django?

Moise Lala
5 min readFeb 5, 2024

--

In the last post, I detailed what has to be saved for an activity application. In this one, I explain how part of it is achieved in Django.

Django, for those who do not know, is a web framework (software designed to support the development of web applications) coded in Python that allows you to develop a web application very fast.

Prerequisite

  • Install Python. Any Python version >= 3.10 will do
  • Create a virtual environment. This is optional, although I love to work in a virtual environment.
  • Install Django 5.0.1 (the latest version of Django will do, as long as it is compatible with Python version >= 3.10)
  • Make sure that Django can be invoked from the terminal

How do you set up the application in Django?

Open the terminal and type the command:

django-admin startproject trackerBe

The command startproject tells Django to create a folder named trackerBe, the project’s name. I call it trackerBe as I will track my activities, and Be stands for back-end. In the trackerBe folder, you will find the following content:

  • manage.py, a file

A folder trackerBe, with content:

  • asgi.py
  • settings.py
  • urls.py
  • wsgi.py

The file manage.py is the command line utility that allows you to interact with Django, such as telling Django to create an app (I will explain what an app is later on).

The other file of interest is settings.py, a configuration file on how your Django web application should behave and the location of files essential for the Django web application, such as security and apps.

There is a command to run and check the web application in Django:

python manage.py runserver

Navigate to 127.0.0:8000 or localhost:8000 on the web browser of your choice. You should see:

Go ahead and run:

python manage.py makemigrations && python manage.py migrate

The command will create tables necessary for the Django application to function correctly. These apps are in the INSTALLED_APPS section of the trackerBe/settings.py file.

What is an app in Django?

An app in Django is a web application that has a meaning in the overall application. This can range from many different things: a page on the web application can be an app in Django; a form on a page can be an app; the data the user manipulates on the web application can also be an app in Django or a combination of them all.

For the activity application, the apps will be used for the data that the user manipulates on the web application. In this article, I mentioned that the operations that a user needs to perform are:

  • Read (I did not mention this in the article)
  • Create
  • Update
  • Delete

These operations are done for an activity, goal, to-do list, and to-do list item.

The Django operation for creating an app is:

python manage.py startapp app_name

The app_name is the name of the app in your Django project.

As a result, the following files are always created:

  • models.py
  • tests.py
  • views.py
  • apps.py

Once an app is generated, Django does not know of it yet. To register the app with Django, you need to add it in the INSTALLED_APPS entry and then run the command:

python manage.py makemigrations app_name

followed by:

python manage.py migrate app_name

This creates a migration folder inside the app_name folder.

Creating the goal app

At the same level as manage.py, open a terminal and run the following command:

python manage.py startapp goal

The file we are interested in is the models.py file.

Add the following content to goal/models.py:

from django.db import models

class Goal(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
start_date = models.DateTimeField()
end_date = models.DateTimeField()
progress = models.IntegerField()
color = models.CharField(max_length=20, default="black")

def __str__(self):
return self.title

The class Goal acts as a template for objects of kind Goal. This tells Django that a goal has a title, a description, a start and end date, a progress, and a color attribute.

Def __str__ is a function that, every time a Goal object is to be displayed in the terminal, rather than showing the object’s memory address, can show the title attribute of the Goal object in a human-readable format.

This also tells Django that when this app is migrated, there should be a table called Goal with specified attributes.

Now, go to trackerBe/settings.py and in the INSTALLED_APP, add the entry:

'goal.apps.GoalConfig',

Migrate goal app:

python manage.py makemigrations goal && python manage.py migrate goal

Creating the activity app

Follow the same steps as for the creation of the goal app:

python.py manage.py startapp activity

In the activity/models.py file, add the following:

from django.db import models
from goal.models import Goal

class Activity(models.Model):
title = models.CharField(max_length=200)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
goal = models.ForeignKey(Goal, on_delete=models.CASCADE)

def __str__(self):
return self.title

An activity object has a title, a start and end date, and a goal attribute. This goal attribute refers to a Goal object. This also means that one goal has many different activities.

Add the following to trackerBe/settings.py

'activity.apps.ActivityConfig',

Migrate the activity app:

python manage.py makemigrations activity && python manage.py migrate activity

Creating the to-do app

The same steps as the other two apps.

Run the command:

python manage.py startapp todo

In the to-do models.py add the following:

from django.db import models
from activity.models import Activity

class Todo(models.Model):
title = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
activity = models.ForeignKey(Activity, null=True, on_delete=models.SET_NULL)

def __str__(self):
return self.title

A to-do can have an activity associated with it or not. As a to-do list is composed of to-do items, as in our definitions, we will also add the to-do item in the same models.py file. As such, add the following to the todo/models.py:

class TodoItem(models.Model):
title = models.CharField(max_length=200)
description = models.TextField(default="")
is_done = models.IntegerField(default=0)
todo = models.ForeignKey(Todo, on_delete=models.CASCADE)

def __str__(self):
return self.title

Add the todo app in the INSTALLED_APPS in trackerBe/settings.py

'todo.apps.TodoConfig',

Run the command:

python manage.py makemigrations todo && python manage.py migrate todo

Create a superuser and Admin site

Django exposes an admin site where you can perform operations such as read, write, update, and delete.

Now, with the apps migrated and the database has tables related to the apps, to create some data, create the superuser by this command:

python manage.py createsuperuser

It will prompt you to type your email, username, and password and to confirm the password.

Register each of the apps with the admin. To do so, open the admin.py in each app folder. For example, to register a goal, open goal/admin.py and add the following:

from django.contrib import admin

from .models import Goal

admin.site.register([Goal])

Navigate to localhost:8000/admin. Log in with your username and password. You should see the admin site with the list of the registered apps.

Interact with each app table by adding goals, activities, and to-do items.

Happy coding!

--

--