In this article, you will learn how to build web applications using Django; the Python framework. We will focus on the project management system which is a system that helps organize and manage projects, tasks, and teams. It tracks progress, assigns responsibilities, sets deadlines, and ensures that everything stays on schedule.
I will break the articles into series, in this 1st series, we’ll cover how to:
- Set Up Your Django Project
- Define Your Models
- Run Migrations
- Register Models with the Admin
Prerequisites
To fully follow up with this tutorial you will need to have Python installed and a basic understanding of Django.
- Setting Up Your Django Project
First, let's create a folder and virtual environment to manage our project dependencies. Run the following code snippets in your terminal:
# create a folder
mkdir project_managent
# create virtual environment
python3 -m venv env
# activate virtual environment
# On Windows use
env\Scripts\activate
# On Linux
source env/bin/activate
Next, install django:
pip install django
After installing django, set up your Django project:
django-admin startproject project_management
cd project_management
Then, inside our project let's create an app called projects
python manage.py startapp projects
After creating your app, add the app to your INSTALLED_APPS
in setting.py
to integrate the app within your project_management project.
# project_management/settings.py
INSTALLED_APPS = [
...
'projects',
...
]
- Defining Our Models
Let's define our models for our project management system. We'll create models for project
and Task
# models.py
from django.db import models
from django.contrib.auth.models import User
class Project(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
owner = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Task(models.Model):
STATUS_CHOICES = [
('todo', 'To Do'),
('in_progress', 'In Progress'),
('done', 'Done'),
]
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='tasks')
name = models.CharField(max_length=100)
description = models.TextField()
assigned_to = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tasks')
due_date = models.DateField()
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='todo')
def __str__(self):
return self.name
- Run Migrations
After defining our models, create and apply the migration to update our database schema(we are using django sqlite3 default database).
# create migrations
python3 manage.py makemigrations
# apply migrations to the database
python3 manage.py migrate
- Register Models with the Admin
Let's register our two models with the admin to manage them through the Django admin interface.
# admin.py
from django.contrib import admin
from .models import Project, Task
admin.site.register(Project)
admin.site.register(Task)
To be able to manage the models in the admin interface, you need to create a superuser.
python3 manage.py createsuperuser
After Following the steps given after running the above command. Now run the server using the following command on your terminal:
python3 manage.py runserver
After running the server visit http://127.0.0.1:8000/admin/ on your browser and log in using the credentials you created above, to access the admin interface.
Once you log in, you should see the project and the task models added. Now you can be able to perform CRUD operations in the admin interface.
In conclusion, With these steps, we’ve laid the foundation for our project management system by setting up the Django project, defining our models, and registering them in the admin interface.
Stay tuned as we continue building this project management system and deepen our understanding of Django!
Top comments (0)