Building a Django To-Do App with Models, Forms, and MySQL
A step-by-step guide to creating a Django To-Do list application using MySQL as the database.
1. Setting Up the Environment
Before we start building the Django To-Do app, make sure you have the following installed:
- Python 3.x: Download and install from python.org.
- Django: Install via
pip
usingpip install django
. - MySQL: Install MySQL Server and create a database for your project.
2. Installing MySQL and Configuring Django
Step 1: Install MySQL Client
To connect Django to MySQL, install the MySQL client:
pip install mysqlclient
Step 2: Create a MySQL Database
Create a MySQL database for your Django project by running:
mysql -u root -p
CREATE DATABASE todo_db;
Step 3: Update Django's Database Settings
Update your settings.py
to configure MySQL as your database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'todo_db',
'USER': 'root',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
3. Creating a Django Project and App
Step 1: Create the Project
To create a new Django project, run:
django-admin startproject todo_project
cd todo_project
Step 2: Create the To-Do App
Create a new app called "todo" by running:
python manage.py startapp todo
Step 3: Register the App
Add the app to INSTALLED_APPS
in todo_project/settings.py
:
INSTALLED_APPS = [
'todo',
]
4. Setting Up Models (Database Structure)
Define a model for your To-Do tasks in todo/models.py
:
from django.db import models
class Todo(models.Model):
title = models.CharField(max_length=200)
completed = models.BooleanField(default=False)
def __str__(self):
return self.title
5. Creating and Validating Forms
Create a form to handle user input in todo/forms.py
:
from django import forms
from .models import Todo
class TodoForm(forms.ModelForm):
class Meta:
model = Todo
fields = ['title', 'completed']
6. Connecting Views and Templates
Step 1: Create Views
Define views in todo/views.py
to display and handle forms:
from django.shortcuts import render, redirect
from .models import Todo
from .forms import TodoForm
def todo_list(request):
todos = Todo.objects.all()
if request.method == 'POST':
form = TodoForm(request.POST)
if form.is_valid():
form.save()
return redirect('todo_list')
else:
form = TodoForm()
return render(request, 'todo_list.html', {'todos': todos, 'form': form})
Step 2: Create HTML Template
Create a template to display tasks and the form in todo/templates/todo_list.html
:
<!DOCTYPE html>
<html>
<head>
<title>To-Do List</title>
<link rel="stylesheet" href="{% static 'styles.css' %}">
</head>
<body>
<h1>To-Do List</h1>
<ul>
{% for todo in todos %}
<li>{{ todo.title }} - {{ todo.completed|yesno:"Yes,No" }}</li>
{% endfor %}
</ul>
<h2>Add a New Task</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Add Task</button>
</form>
</body>
</html>
7. Running Migrations for MySQL
To create and apply migrations for MySQL, run the following commands:
python manage.py makemigrations
python manage.py migrate
8. Testing the Application
To run the Django development server and test your To-Do app, use:
python manage.py runserver
Open your browser and navigate to http://127.0.0.1:8000/todo/ to see the app in action.
0 Comments