BlogEducationProgramming

Create a Simple Todo App With Django

To create a todo app with Django, you’ll need to follow these steps:

Install Django and create a new project:

pip install django
django-admin startproject todo

Create a new app within the project:

python manage.py startapp tasks

Define the model for the to-do tasks in tasks/models.py A to-do task might have a title, description, and completion status. You can use the following code as a starting point:

from django.db import models

class Task(models.Model):
     title = models.CharField(max_length=200)
     description = models.TextField()
     completed = models.BooleanField(default=False)

    def __str__(self):
       return self.title

Run the database migrations to create the database table for the tasks:

python manage.py makemigrations
python manage.py migrate

Create a view function in tasks/views.py to display a list of tasks and a form to add new tasks. You can use the following code as a starting point:

from django.shortcuts import render
from .models import Task
from .forms import TaskForm

def task_list(request):
    tasks = Task.objects.all()
    form = TaskForm()
    if request.method == 'POST':
       form = TaskForm(request.POST)
       if form.is_valid():
          form.save()
          return redirect('/')
    context = {'tasks': tasks, 'form': form}
    return render(request, 'tasks/task_list.html', context)

Create a template for the task list view in tasks/templates/tasks/task_list.html. You can use the following code as a starting point:

<h1>Todo List</h1>

<form method="POST" action="{% url 'task_list' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Task">
</form>

<ul>
{% for task in tasks %}
<li>{{ task }}</li>
{% endfor %}
</ul>

 

Create a form for the add task form in tasks/forms.py. You can use the following code as a starting point:

from django import forms
from .models import Task

class TaskForm(forms.ModelForm):
     class Meta:
           model = Task
           fields = ['title', 'description']

Create a URL pattern for the task list view in tasks/urls.py:

from django.urls import path
from . import views

urlpatterns = [
path('', views.task_list, name='task_list'),
]

Include the URL patterns of the tasks app in the main urls.py file:

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

urlpatterns = [
path('tasks/', include('tasks.urls')),
path('admin/', admin.site.urls),
]

Run the development server to test the todo app:

python manage.py runserver

You should now be able to view the to-do list at http://127.0.0.1:8000/tasks/ and add new tasks using the form.

To mark a task as completed, you can add a checkbox next to each task in the template and update the completed field in the database using a form submission. You can also create separate views for listing completed and incomplete tasks and add links to switch between the views.

For more information on building web applications with Django, you can refer to the official Django documentation (https://docs.djangoproject.com/) or the Django tutorial (https://docs.djangoproject.com/en/3.1/intro/tutorial01/).

Muhammad Sameer
Author: Muhammad Sameer

I am a talented, ambitious, and hardworking individual, with broad skills and experience in digital and printed marketing, social media, and leading projects. Furthermore, I am adept at handling multiple tasks on a daily basis competently and at working well under pressure. A key strength is communication; building strong relationships with people in order to deliver the best results. Recently, I completed some projects, including Business and Design modules as a task. I am able to handle multiple tasks on a daily basis. I use a creative approach to problem-solving. I am a dependable person who is great at time management. I am always energetic and eager to learn new skills. I have experience working as part of a team and individually also have an experience in different technology

Muhammad Sameer

I am a talented, ambitious, and hardworking individual, with broad skills and experience in digital and printed marketing, social media, and leading projects. Furthermore, I am adept at handling multiple tasks on a daily basis competently and at working well under pressure. A key strength is communication; building strong relationships with people in order to deliver the best results. Recently, I completed some projects, including Business and Design modules as a task. I am able to handle multiple tasks on a daily basis. I use a creative approach to problem-solving. I am a dependable person who is great at time management. I am always energetic and eager to learn new skills. I have experience working as part of a team and individually also have an experience in different technology

Leave a Reply