Django Tasks Manager - Free PyPi Library
Tags: django, webdev, celery, opensource
Published: 2022-10-14 15:46:57

---
title: Django Tasks Manager - Free PyPi Library
published: true
description: A free sample project that integrates Django-TM, an open-source package for Django and Celery
tags: django, webdev, celery, opensource
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umpiv83ciweralhmcp2d.jpg
canonical_url: https://blog.appseed.us/free-sample-django-and-celery/
---
Hello Coders! 

This article presents an [open-source sample](https://github.com/app-generator/sample-django-tasks-manager) that explains step-by-step how to use [Django Tasks Manager](https://github.com/app-generator/django-tasks-manager) library in a new project. The [commits](https://github.com/app-generator/sample-django-tasks-manager/releases) are made to highlight each step of the implementation starting from an empty directory up to the final phase when the project manages the tasks through UI controls. *Thanks for reading!*

- 👉 [Django & Celery Manager](https://github.com/app-generator/django-tasks-manager) - the library used
- 👉 [Tasks Manager Sample](https://github.com/app-generator/sample-django-tasks-manager) - the implementation sample
- 👉  [Django Tasks Manager](https://appseed.us/developer-tools/django-tasks-manager/) - `Product Page` (for support)

---

## **What's in the box**

The library used by the sample is powered by **Django and Celery**, a popular task queue manager, and provides a simple way to execute background tasks in full control: start/stop, view output, and runtime logs. Here is the full list of library features:

- ✅ Create/Revoke `Celery Tasks`
- ✅ `View LOGS` & Output
- ✅ `Minimal Configuration`
- ✅ Installation via `PyPi`
- ✅ Available [TASKS](https://github.com/app-generator/django-tasks-manager/blob/main/django_tm/tasks.py) (provided as starting samples)
- ✅ Task `users_in_db()` - List all registered users
- ✅ Task `execute_script()` - let users execute system [scripts](https://github.com/app-generator/django-tasks-manager/tree/main/django_tm/celery_scripts)

In the end, this UI exposed by the tool is similar to this:

![Django Celery Integration - Tasks Board](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3l17094y8z157dtftf8.png)

--- 

![Django Celery Integration - Task Runtime Log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z124onehao3pzouk7ych.png)

---

In case this library sounds useful, the integration steps for any Django project are presented below.  

An important aspect is the **Redis** service dependency required by **Celery** used to manage and tasks state tracking in real-time.

--- 

> 👉 **Step 1** - Install `django-tasks-manager` via PIP

The recommended way to do this is to use a virtual environment that isolates the installation of the library.

```bash
$ pip install django-tasks-manager 
```

Once the installation is finished the next step is to create two directories used by the library:

- `celery_logs` - used to log the runtime execution of the tasks
- `celery_scripts` - the location where the library search for scrips to be executed

The package ships a few simple scripts as a proof of concept available for download from the [public repository](https://github.com/app-generator/django-tasks-manager/tree/main/django_tm/celery_scripts).

---

> 👉 **Step 2** - Update `app routing`

```python
# core/urls.py

from django.urls import path, include     # <-- UPDATE: Add 'include' HELPER

urlpatterns = [
    ...
    
    path("", include("django_tm.urls")),  # <-- New Routes

    ...
]
``` 

---

> 👉 **Step 3** - `Update Configuration`, include the new APPS

```python
# App Settings, truncated content

INSTALLED_APPS = [
    ...                  
    'django_tm',              # Django Tasks Manager   # <-- NEW
    'django_celery_results',  # Django Celery Results  # <-- NEW
]
```

---

> 👉 **Step 4** -  Update Configuration, `include page templates`

```python
# App Settings, truncated content

TEMPLATE_DIR_TASKS = os.path.join(BASE_DIR, "django_tm/templates") # <-- NEW

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',   
        'DIRS': [TEMPLATE_DIR_TASKS],                              # <-- NEW
        'APP_DIRS': True,
    },
]
```

---

> 👉 **Step 5** - Update Configuration, New `CELERY_` Section

```python
#############################################################
# Celery configurations

# BASE_DIR points to the ROOT of the project
# Note: make sure you have 'os' object imported
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Working Directories required write permission
CELERY_SCRIPTS_DIR        = os.path.join(BASE_DIR, "celery_scripts" )
CELERY_LOGS_DIR           = os.path.join(BASE_DIR, "celery_logs"    )

CELERY_BROKER_URL         = os.environ.get("CELERY_BROKER", "redis://localhost:6379")
CELERY_RESULT_BACKEND     = os.environ.get("CELERY_BROKER", "redis://localhost:6379")

CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT    = 30 * 60
CELERY_CACHE_BACKEND      = "django-cache"
CELERY_RESULT_BACKEND     = "django-db"
CELERY_RESULT_EXTENDED    = True
CELERY_RESULT_EXPIRES     = 60*60*24*30 # Results expire after 1 month
CELERY_ACCEPT_CONTENT     = ["json"]
CELERY_TASK_SERIALIZER    = 'json'
CELERY_RESULT_SERIALIZER  = 'json'

#############################################################
```

---

> 👉 **Step 6** - `Migrate the database` & `start` the app

```bash
$ python manage.py makemigrations
$
$ python manage.py migrate
$ 
$ python manage.py runserver
```

---

> 👉 **Step 7** - Start the `Celery` manager (using another terminal)

```bash
$ celery --app=django_tm.celery.app worker --loglevel=info 
```

The superusers now can connect to the UI and orchestrate the tasks visually at this address: `http://127.0.0.1:8000/tasks` 

![Django Celery Integration - Visualize Finished Tasks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h16erqcf323nispuaitu.png)

---

> `Thanks for reading!` For more resources, feel free to access:

- 👉 Free [Support](https://appseed.us/support/) via Email and Discord
- 👉 More [free starters](https://appseed.us/admin-dashboards/open-source/) crafted in different technologies

---

> 🚀 **PROMO** (contains `affiliate links`)

In case you're a junior developer or know one, this [PROMO Bundle crafted, and **Discounted with 85%** by `Creative-Tim` might be useful. The package includes a rock-solid collection of premium assets (**Kits & Dashboards**) that can be used to build eye-catching portfolios and web apps in no time.

> 👉 [Junior PROMO Bundle](https://www.creative-tim.com/product/buy/bundle/junior-bundle?AFFILIATE=128200) - **24 PREMIUM Kits** & Designer Files 

![Promo Bundle for Junior Developers - By Creative-Tim.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlyjblaminbg8j6ixalu.png)