Are you ready to dive into the world of web development? In this quick tutorial, you’ll learn how to create a simple yet effective To-Do List application using Django, one of the most popular web frameworks in Python. Let’s get started and unleash your inner developer! 💻✨
### Step 1: Setting Up Your Django Environment
Before we begin coding, ensure you have Python and Django installed. If you haven’t installed Django yet, you can do so using pip:
“`bash
pip install django
“`
Once you have Django installed, create a new project by running:
“`bash
django-admin startproject todo_app
cd todo_app
“`
### Step 2: Create Your To-Do List App
Now, let’s create our To-Do List app:
“`bash
python manage.py startapp tasks
“`
Add your new application to the project settings. Open `todo_app/settings.py` and add `’tasks’,` to your `INSTALLED_APPS` list.
### Step 3: Model Your Database
In your `tasks/models.py`, we’ll define a simple model for our tasks:
“`python
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=200)
completed = models.BooleanField(default=False)
def __str__(self):
return self.title
“`
Next, let’s create the database tables:
“`bash
python manage.py makemigrations
python manage.py migrate
“`
### Step 4: Creating Views
In `tasks/views.py`, let’s create views to handle our To-Do List:
“`python
from django.shortcuts import render, redirect
from .models import Task
def index(request):
tasks = Task.objects.all()
return render(request, ‘tasks/index.html’, {‘tasks’: tasks})
def add_task(request):
if request.method == ‘POST’:
title = request.POST.get(‘title’)
Task.objects.create(title=title)
return redirect(‘index’)
def complete_task(request, task_id):
task = Task.objects.get(id=task_id)
task.completed = True
task.save()
return redirect(‘index’)
“`
### Step 5: Configure URL Routing
Open `tasks/urls.py` and link the views with URLs:
“`python
from django.urls import path
from .views import index, add_task, complete_task
urlpatterns = [
path(”, index, name=’index’),
path(‘add/’, add_task, name=’add_task’),
path(‘complete/
]
“`
Don’t forget to include these URLs in the main project’s `urls.py`:
“`python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(”, include(‘tasks.urls’)),
]
“`
### Step 6: Create Your Template
Create a folder named `templates` inside your `tasks` folder and inside it create `index.html`:
“`html
My To-Do List
-
{% for task in tasks %}
- {{ task.title }} – {% if task.completed %} Completed {% else %} Complete {% endif %}
{% endfor %}
“`
### Step 7: Run Your Server
Finally, let’s run our server to see the To-Do List app in action:
“`bash
python manage.py runserver
“`
Visit `http://127.0.0.1:8000/` in your web browser, and voila! You’ve built a To-Do List application in under an hour! 🕒💡
### Conclusion
Congratulations on completing your first Django project! 🎉 From here, you can enhance your To-Do List with features like user authentication, deadlines, or even a fluffy UI. The possibilities are endless!
🌟 Happy coding! 🌟
#Django #Python #WebDevelopment #ToDoList #CodingTutorial #LearnDjango #PythonTutorial