Title: Building a Django Todo App
Summary:
we will guide you through the process of creating a Django Todo App. By following along, you'll learn how to set up a virtual environment, install Django, create a new project, handle static files and templates, create a todo app, define models, apply migrations, and more.
python -m venv venv
Activate the virtual environment:
venv\scripts\activate # For Windows
source venv/bin/activate # For macOS and Linux
pip install django
django-admin startproject todo_list
mkdir static
Update settings.py
:
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']
mkdir templates
Define base.html
in the templates directory.
Update settings.py
:
'DIRS': [BASE_DIR / 'templates' ],
django-admin startapp todo
Add 'todo',
to INSTALLED_APPS
in settings.py
.
Create a views.py
file in the todo
app:
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
Define URL routes in urls.py
:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Include the app's URLs in the project's urls.py
:
path('', include('todo.urls'))
Define the Task
model in models.py
:
from django.db import models
from django.contrib.auth.models import User
class Task(models.Model):
title = models.CharField(max_length=255)
description = models.TextField(null=True, blank=True)
completed = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return self.title
class Meta:
ordering = ['completed']
Register the Task
model in admin.py
:
from django.contrib import admin
from .models import Task
admin.site.register(Task)
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
Restart the Django server:
python manage.py runserver
Navigate to http://127.0.0.1:8000/
in your web browser.