Task Management App Tutorial with Python

Tags: python, programming, technology, tutorial
Published: 2022-01-19 22:59:55

In this tutorial, we'll first build a simple to-do app in Django using Python. Then we'll explore some ways we can visualize the resulting data in our database client. Want to build an analytics dashboard or twitter scraper instead? Check out these previous tutorials:

- [Build an Analytics Dashboard with Django and Arctype](https://arctype.com/blog/analytics-dashboard-django/)

- [Build Twitter Heat Maps with Tweepy, Django and Arctype](https://arctype.com/blog/twitter-api-python/)

### Prerequisites

To fully understand this tutorial, you will need the following:

- [Python 3.8](https://www.python.org/downloads/) or newer
- A basic understanding of [Django 3.0 or newer](https://docs.djangoproject.com/en/4.0/intro/overview/)
- An IDE ([PyCharm](https://www.jetbrains.com/pycharm/features/), [VS Code](https://code.visualstudio.com/docs)).
- A SQL client like [Arctype](https://arctype.com)
- Basic working knowledge of [SQL](https://sqlbolt.com/)

## Python setup

### Installing Python

First, let's install Python using [this link](https://www.python.org/downloads/). Then, we can select our OS and download it. This tutorial was built on Windows. We can run the command `Python --version` to check if Python is successfully installed on our system. If Python is installed, we get a response as shown below:

![Screenshot of a successful Python install.](https://lh3.googleusercontent.com/ZDMUI2glWJtZPNDk_xdnxjONe6-lW6TC3SEZeG193Bub-DHam2nhkoW2GA-K5rnkfe2MbQWTgARo53daANiLZeJ1cmSZTlvaSlRtFfaKWZQQIqpBtvoWmkHW-4JBEhs0wnEvin_g)

### Creating a virtual environment

Let's create an isolated [virtual environment](https://docs.python.org/3/library/venv.html) for this project. Virtual environments help us avoid installing dependencies globally on our system. This helps to make sure that the dependencies of other projects won't be included within the dependencies of our current project, even though they're all on the same local machine. To do that run the following command.

![Screenshot of a virtual environment.](https://lh3.googleusercontent.com/SB-UqqY5hTLmo54_liPAmH8C_PIVc6K5PTJipEomow7A4sk214o63WRRftixrm5lXOBe2_kzSEDi5DRcPGAcOVw4n-J71V8KKk_sLJvw-upV-VcXnZbpqErhUNLW5zF_CUI4v40m)

Next, activate the virtual environment by navigating to the base directory of our new virtual environment (in our example, it has the name "env"). Then, navigate into the Scripts folder and run the executable file "activate."

![Screenshot of a command line.](https://lh3.googleusercontent.com/Ov550gmHo0jVqK92R5JAy08uYTtfGAdnQWe_XXnrcgdkbQKQ48GscrI2gj9gGbMeVfNYuZhQOyMkM-iCTSEZ_52lCxDYvgpPKj0DJh30HBurI24MLQpTndUUgderFhpfoljSZrnO)

## Django Setup

You can [install Django Web Framework](https://www.djangoproject.com/download/) with pip and then follow these steps.

### Create a project

Now, let's create the project. We can do this by running the command `django-admin startproject` followed by the name of the project (in this case, `taskmanagement`). The full command should look something like this:

```
django-admin startproject taskmanagement
```

Here, we can name the project however we like, but be sure to be descriptive.

### Create an app

Let's navigate to the `taskmanagement` folder and create an application. To do that we use the command `python manage.py startapp` then the name of the app, which in our case we've named `task_management_app`.

![Screenshot of Python code.](https://lh6.googleusercontent.com/-DzFjj8Sg61ge1CKwhKkZulLzfdodEmjgs93dyXfsK6RuX_c6lGonulm0D7S2PLLzu0F_pTz7uaIIk_j-kUX7Io1C-sLmKQ_WQR3pdPFn_ygn6rt5aFW5vT84eZlnSw9Xp2z4Xrd)

The app is created successfully. We can now open the project in our favorite IDE.

### Migrations

In Django, we use migrations to move changes we make to our models (adding a field, deleting a model, etc.) into our database schema. First, we need to make migrations to our database. Run the command `python manage.py makemigrations`. `makemigrations` are used to create new migrations based on the changes made to the models in a project. The output is as shown below.

![Screenshot of Python code](https://lh4.googleusercontent.com/3sd-lPpah-N9_9WV5tMom_RaJo--oLGv4DSiEHjEg_wD-rJipfVJtdP-jLrLrAIjet9g-JpFlzPOL-Rd5Ovq0Hyo4FE6cGp5MTp6eDkg6taffStg-yuZ5WtUYOAfgKmJ6QyBpuXF)

Now let's run our app to see if we have successfully installed and our project properly set up. To do this, we are to change the directory into our project and run the command `python manage.py runserver`:

![Screenshot of server spinup.](https://lh5.googleusercontent.com/lvgNzmE501CAK5s92zm5MhvX4cTlwXXF8cDpWoxE3gDPLKPLwOa3XgxOwK0h7kU7vAXbZgzEHvZgnWRu_J-mlEjbrjtjPyz96vEzQXr1YnTELRoCQ6vRpKG0sW-RpvbWVDtI5azQ)

We have started our development server at [http://127.0.01:8000/](about:blank)! So we can now open our browser to access our app using this URL. If everything works fine, the screen with the same image below is displayed as the default Django page.

![Default project landing page.](https://lh4.googleusercontent.com/sWPPYvhFN7AP7lPjI_NdgslgQeJLDuDHMUxOAAiGuEk36b2V2C9raqEs4H--6lp9aEKXeCcgYEfSp--blS4_OUkqxnyjEdmOND8Ht_-QiAvu-kXIcJAnWJ9kkv9T3sALfYah5oV9)

Now we want to make some configurations inside the `settings.py` file. The primary thing we will do is add the app's name in the installed apps, as seen directly below, and then add the MySQL database to Django.

![Adding app to the list of installed apps.](https://lh5.googleusercontent.com/wnwcqyvAiP7JjUJda_dcbimRrbw6pAVp9nFyo1rM3zj26E8m58JEMEy9P_wRoZJfLh_m91XxvlTArotlnyz6w3xbA3Z95GGd3Y93wlz9XSf2MuYWta64Y4bcNiuMMtHQadl97Oi2)

### Editing Settings.py to add MySQL to Django

In your `settings.py` file, edit the code below and replace it with the details required to connect to your SQL server:

```
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database_name',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'USER': 'username',
        'PASSWORD': 'password',
    }
}
```

## Todo list app data model

Next, we want to create a model. This model will determine how the task will be stored on the database. The task will have seven properties, a `title`, a `description` of the task, a `startTime`, an `endTime`, a `completed` status, and a `created on` and an `updated on` column to mark when our task was first created and updated. 

Note: our `startTime` and `endTime` use the `DateTime` class. This will hold the date and time in which the task was started and completed respectively. We have set `created_on` field's `auto_now_add`property to true. This automatically fills this field with the current date and time in which the task was created.

![Screenshot of defining the fields.](https://lh6.googleusercontent.com/cTD33gZxcANzp4YWBq-goJ8AQ_75pqnbHobbkedwJjtySSYub8ElFp5vaE4A_VojIfrlYZCFRkVOYvPBmMwD6IXWBQVV8YJ6LcnLXCKRka0hCtiF5ah2ZqiaiCNfb3J1qV8fUS7S)

Now let's go ahead and make migrations. Any time changes are made to our models, we need to make migrations. Once that is completed, we will find a new file inside the migrations folder as shown below.

![Screenshot of Python code.](https://lh5.googleusercontent.com/XDCEXJdkzFTkhQsm0xXzR0c3yxrWBygwnNMmkDbtXdsWBG31Nzoe5SA9WRhgWnUluMjs7aSp2W7RP98GhA-nsX5ECswH2LWUxLG49_Ut4aV3sjEN_tpiOXh_UeSEDl8vXdkpdmay)

We didn't write this code manually. This is the SQL code that Django has handled for us. Next, we migrate with python manage.py migrate. Migrate is responsible for applying and reapplying migrations.

![Running the migrations.](https://lh6.googleusercontent.com/n_CzJzlizg3YinGEqV9SYgpV2lmW36YgpUdo9SjQYcTSPhZ3UpqhYTDJ55qNpte2DkQ7jTqGVnAzrIbV4P7CMd7uFFWz0Y0XVhv0z7bN6oYrABxH2JqsAk7G7yh7OW1Fz3tivzF3)

Now we test that the CRUD operation was successful by registering the task on the admin panel as follows:

```
from django.contrib import admin
from .models import Task

class TaskAdmin(admin.ModelAdmin):
      list_display = ("title", "description", "startTime",  "endTime", "completed", "created_on", "updated_on")

admin.site.register(Task, TaskAdmin)
```

![Screenshot of IDE](https://lh6.googleusercontent.com/rYVbsJBUkbLGV0-QXtvriaYBd8-a2l99XXEl-JIgfKlODzdjBe6C3sdXJSBCV64dq2iOnBVhDEA6xvGe4EHcXvKhR40LBbAimY_IdDWYkB49R0lYkoDEucbk_cZ8spZV50cww9qc)

## Task management API

To build the API we will install and use the [Django REST Framework](https://www.django-rest-framework.org/#installation).

### Creating a superuser and adding the rest_framework

Let's create a superuser with the following command: `python manage.py createsuperuser`

![Screenshot of creating a superuser](https://lh4.googleusercontent.com/EjwQHeein3lJM6DHRwlsVG1GY7S8bz-HHjiRtI4Z1lrUJ1QBN7VI-ZVam8nxNvr4dn_UFQEft6afACgM7b4jt7dGNS8V7w5-BNCrxo9yBgx8xKruTGMIvkMqF-s0rTX7ElSN18wE)

Next, we'll need to add the Django `rest_framework` to the list of installed apps.

![Screenshot of IDE](https://lh4.googleusercontent.com/q2tH2gy7BRS_pZ7QXBYrowwAD7vOK5tHa__olFnoY2PXI8-lf42GPvitqawnrMCv_7doKLLVz7limcuxLhwIQtWvY6I9cnyzSts4oBmPw_ExLrdf_dfRqYB6_8OsbOXOF41j1SL7)

### Creating the serializers

JSON stands for JavaScript Object Notation. Serializers convert the model class to JSON so that the frontend can work with the received data.

```
from django.db.models.fields import Field
from rest_framework import serializers
from .models import Task


class TaskSerializers(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = ("id", "title", "description", "startTime", "endTime", "completed", "created_on", "updated_on")
```

![](https://lh5.googleusercontent.com/0bxoIsvHhdH0XN7iEG6V5v9M6LBBcsF88cofds90ri1f145fzLCE_A1ufozP3lSKrmKYhjtX94aJfbqcxJ8V-8Bd_jlm82cp-9xrzgaNjzZhA2bzAPLt6v75h2n9ZYsO2A32JED3)

### Creating the view

The view is simply a Python function that takes an HTTP request and returns a response. This response may be the HTML contents, for example. The view itself houses the business logic that is necessary to return a response to a client that makes an HTTP call to it.

```
from django.db.models.query import QuerySet
from django.shortcuts import render
from rest_framework import viewsets
from .serializers import TaskSerializers
from .models import Task

# Create your views here.

class TaskView(viewsets.ModelViewSet):
    serializer_class = TaskSerializers
    queryset = Task.objects.all()
```

![Screenshot of IDE](https://lh4.googleusercontent.com/pbn0rSUQAUmSNd7whsGCEeMIaVzVV1PG4kt767aKv804VrCxDQaWwpXxpdqp5SHqk0pbwv2WrSRgh72Bsz0eXN6LJz5fyu14gmJ5Ybi3aJSbqmDGaedPT46JVpHwjd_sc9jv75lj)

### The URL

The URL is used to map path expressions to Python functions (your views). It first receives HTTP requests and routes them to the right matching function in views.py to handle the request. Now let's go to the URL that is in the project folder and add the following code:

```
from django.contrib import admin
from django.urls import path
from django.urls.conf import include
from task_management_app import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register(r'tasks', views.TaskView, 'task')

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include(router.urls)),
]
```

![Screenshot of IDE.](https://lh3.googleusercontent.com/KrpY7rzFxhQ1oStNLf2fUd6ltpg0jJ9z6Nhd2tQhGmuP7tlPfiqvA1HfcqGGKggSR4UxOJVRTwfwHUq20J-oxL0ei3KXgEHGwJU1QX8kjc-_M78L0q6Ep2CgBMTVzRTY1XssRplY)

### Accessing our endpoints

To access our endpoint run your app and visit [http://127.0.0.1:8000/api/tasks/](http://127.0.0.1:8000/api/tasks/) and add a task.

![Screenshot of application.](https://lh3.googleusercontent.com/huFHKyfAVQXUlrbNz2sb1XsKhHuxHtMlgsRTBGReu0t7AqdCSXSkDmdZavKTA7Mhui_dXGN113ot3KdRzoZDo6XuxfzLFdNNwb9zid1wuEYadx0lyJwqcjZcRpdPh9ftDX51YNOi)

Boom! We have it on our task manager. We can get a particular task by appending the `Id` of the task to the URL. We can also update or delete a particular task.

This section has taught us how to build a task management app that performs CRUD operations. Now, we will use a SQL client to visualize the data on our database.

## Visualize and query task data

Next, we select the SQL type we are most familiar with. In the course of this article, we would be [connecting to a MySQL](https://arctype.com/mysql/drivers/python-mysql) relational database.

![Connecting to the DB.](https://lh5.googleusercontent.com/p1Hjw2BHPYncDTNiCbymMNjNoTiEDds7ELlmTPe1YqlysRpO0DiJ_kSiINinBs2NgyNr6pwxhH2Qek6e-Jd5855FB1G3aN0iNp8dYKjZGT66RfdOFnWz4nt1gM16NzX2ayYd62ue)

### Visualizing tables

I made a sample data .csv for this tutorial. First, I created a Google sheet, exported it to a .csv, and then imported it. This data table contains columns: `id`, `title`, `description`, `completed`, `created on`, `startTime`, `endTime`, and `updated on`. This is our exported data set in Table view:

![Screenshot of Arctype.](https://lh6.googleusercontent.com/Afpm22PfKoGPePCvXj1pWJqVnUC_08XjX5Q1M-dT7D-Ml-iW6xKV16Dv-qiDv8V_feWCFnuE3cTDDCg9vVHJT2wAbVI4tWEWRfJluv9mTN9fMnIKdhAvlLEU5w_8-DxKzBTvbJrD)

### Creating a dashboard to group together activities

Now, we are going to display our list of activities. First, we'll write an SQL query that will display our data set. Click on the `Queries` tab by the sidebar and select `Create query` button.

![Screenshot of Arctype](https://lh5.googleusercontent.com/OEqF0wgjvC_q_ZTSEPfa7edY8roaoS12ir0a3P6Lrzk8PxcTBLur8mdqYfuwQTDZLHGhsOU2YEWOkpassS3YyhNr0SWIDz1jp59vtFeFXF5tyxQ8XqM6FojHIaKxQXTVgzQvHm4b)

We can start by renaming our query and save it in the editor's heading:

```
SELECT COUNT(*) AS tables_priv FROM `excel_for_arctype_data_sheet_1_1`;
```

The result we get should look like the screenshot below:

![Screenshot of Arctype](https://lh5.googleusercontent.com/k6Wi3mjB80wi_WR5zTkmryHlKLrWx2TcWtCMrlJNxc54NOODAHg6IoG9vz41zwxJvkQjb7fTmdlGXB3DgcUl2jAg_Iiq63u9YyO0gxqagxseVtbja26Ej-k24zlhColaQh7BQzsF)

Next, we can now click on the `Dashboard` tab to create a new dashboard. Then, we rename our dashboard with our preferred name.

![](https://lh3.googleusercontent.com/IF2p0JCMTuQbdu3Fk3D7hdbZoOS51zoGZShaZZ_NGDP8rdduFlofT90RcGnJDa-6VYV0dmuW-r0W8QmVizBcdMuAdCbzVqoblBTcnk0RqbdknacBiMP0_hjATywS5X4AzZK-Z2C4)

Next, click on "Add tab" and select `Chart`:

![Adding a chart.](https://lh3.googleusercontent.com/jd-lVq6ZJuEs1x3dqmzWW1vLLA5qivZrvqfcRvkAl3vfHyuOraSkEzunC1Ujor8IzXMcscY9nwW3ooNLyYcUoO7e-zDNZvT_moJlWA41i7T_FDMm6sZaHPRQNFxaCd2CH_PAHGIB)

Then, click on the select chart data and select `activities_count`:

![Screenshot of Arctype.](https://lh5.googleusercontent.com/QXap7ETi8agnolusHEtj75ladtVVEgQP0_L663qrNTvwbq-9Tqnm0GJhyryMbW5AP49BxM9dvE-7Mkos9B9WOjAkYn1JRNw22FukEsIQsucqRAxfI9TyqmYhrVtVsji4fS7HkhAe)

Change the title of the dashboard component and select `Score Card` as the chart type. Then, we can now drag the `table_priv` column to be displayed.

![Screenshot of Arctype.](https://lh4.googleusercontent.com/kLHJHG_1NHbml55MMBhQmDqk-KAxWc52ZEJeHnbnLB-odeMV5Mmb2CE8-wGnFmI-ZY4GjN8i5VEppZw-CBJ5h2_EpPxcQ5VMWyWayeAiVdMA2mXBqNzC262A8THUxl9jranTvIMU)

Next, we create a table component to display the activities that were most productive.

Create a new query called `activities_complete`. We can then carry out the following commands in it:

```
SELECT 
    SUM(completed) AS list_of_completed_activities 
FROM 
    `excel_for_arctype_data_sheet_1_1` 
WHERE 
    title = {{Atitle}};
```

![Screenshot of a query in Arctype.](https://lh5.googleusercontent.com/vQlzhktM_a1i43nPzyWxPWLL3grlQPQ7CC2jmILe0NDyETEtPet7zmDNJ0BuhNqN8R5qcAomNqDrbnmpOvVl_MrQ4k1Zavfme36bdP4AFbmgtymhYt5DJ7bnizYE_LJYFNG6KTkP)

Then, click on the `add` button from the activity dashboard section we already created and select `table`:

![Screenshot of Arctype](https://lh4.googleusercontent.com/frdHVIwAuJ5MrU87LO0AQyJEAR2HJce8nAcRDfH0O-_1APOqLYP2FOJH8uBiqUyBd84BsljSfzCqqEv8_2vz55LQkJYAtrg2ixsQ8EiAKXstkC2khThdz7HQhAmsx8FFCD0-mVup)

Lastly, we click on the select chart data button and select the `Title` query we created earlier.

![Screenshot of Arctype](https://arctype.com/blog/content/images/size/w1000/2022/01/Screen-Shot-2022-01-19-at-10.44.09-AM.png)

### Creating a pie chart

Here, we are going to create chart components with Arctype to display our datasets in pie charts. 

We start the process by using our previous query command. Then, simply select the chart type and change it to `Pie Chart` and drag the `tables_priv` to the column category.

### Mixing different types of charts in one dashboard

Here, we are going to create chart components with Arctype to display our datasets in bar charts. We repeat the same process as we did with pie charts.

We change the chart type to `Bar Chart` and drag `tables_priv` to the column category. Then, we configure the chart to match whatever settings we want.

![](https://arctype.com/blog/content/images/size/w1000/2022/01/Screen-Shot-2022-01-19-at-10.44.09-AM.png)

## Conclusion

This article was divided into two sections. First, we learned how to use a Python framework (Django) to create a task management system. Secondly, we explored the data model of our task management app using a SQL client. While we did that, [Arctype](https://arctype.com) to visualize our tables and columns using queries and dashboards. We also learned how to showcase our dataset into pie charts, bar charts, area charts, and line charts.