Final Project - potatoscript/django GitHub Wiki

Final Project – Build a Mini Blog or Todo App 🚀

In this section, we’ll guide you through building a Mini Blog or Todo App using Django. This project will combine everything you've learned, such as setting up models, views, templates, forms, and implementing CRUD (Create, Read, Update, Delete) operations. By the end of this project, you’ll have a fully functional app that can store, update, delete, and display posts or tasks.


🛠️ Step 1: Set Up Your Django Project

1.1: Start a New Django Project

Open your terminal or command prompt, and navigate to your desired directory. Run the following commands to create a new Django project:

django-admin startproject mini_blog_project
cd mini_blog_project

1.2: Create a Django App

Now, create a Django app within your project. Let’s call it blog for the Mini Blog or todo for the Todo app:

python manage.py startapp blog

If you’re building the Todo app, just replace the name blog with todo throughout the tutorial.

1.3: Add the App to INSTALLED_APPS

Next, go to the settings.py file of your project (mini_blog_project/settings.py) and add the app to the INSTALLED_APPS list:

INSTALLED_APPS = [
    # other apps
    'blog',  # or 'todo'
]

🧩 Step 2: Define the Models

In the models.py file of your app (blog/models.py), define the models for the blog or todo tasks.

2.1: Blog Model Example

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title

2.2: Todo Model Example

from django.db import models

class Task(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField(blank=True)
    completed = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

🧩 Step 3: Migrate the Database

Once your models are defined, you need to create and apply migrations to create the corresponding tables in the database.

python manage.py makemigrations
python manage.py migrate

🧩 Step 4: Create Views to Display Data

Now, let’s create views that will allow users to interact with the data.

4.1: Define Views for Listing Posts/Tasks

In views.py (blog/views.py), define a view to display all posts or tasks:

from django.shortcuts import render
from .models import Post  # or Task for Todo App

def post_list(request):
    posts = Post.objects.all()  # or Task.objects.all() for Todo
    return render(request, 'blog/post_list.html', {'posts': posts})

For the Todo app, just replace Post with Task in the code above.


4.2: Define a View for Creating a New Post/Task

Add a view to handle the creation of new posts or tasks:

from django.shortcuts import render, redirect
from .models import Post  # or Task for Todo App
from .forms import PostForm  # or TaskForm for Todo App

def post_create(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('post_list')
    else:
        form = PostForm()
    return render(request, 'blog/post_form.html', {'form': form})

For the Todo app, just replace Post with Task and adjust accordingly.


4.3: Define a View for Editing a Post/Task

Create a view to edit existing posts or tasks:

from django.shortcuts import render, get_object_or_404, redirect
from .models import Post  # or Task for Todo App
from .forms import PostForm  # or TaskForm for Todo App

def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)  # or Task for Todo App
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)  # or TaskForm for Todo App
        if form.is_valid():
            form.save()
            return redirect('post_list')
    else:
        form = PostForm(instance=post)  # or TaskForm for Todo App
    return render(request, 'blog/post_form.html', {'form': form})

4.4: Define a View for Deleting a Post/Task

Create a view to delete posts or tasks:

from django.shortcuts import get_object_or_404, redirect
from .models import Post  # or Task for Todo App

def post_delete(request, pk):
    post = get_object_or_404(Post, pk=pk)  # or Task for Todo App
    post.delete()
    return redirect('post_list')

🧩 Step 5: Create Forms for Data Entry

We’ll now create forms to handle the data input.

5.1: Create a Form for Creating or Editing Posts

In forms.py (blog/forms.py), create a form for the Post model:

from django import forms
from .models import Post  # or Task for Todo App

class PostForm(forms.ModelForm):
    class Meta:
        model = Post  # or Task for Todo App
        fields = ['title', 'content']  # or ['title', 'description'] for Todo

🧩 Step 6: Set Up URLs

You need to wire everything together by setting up URLs for your views.

In urls.py (blog/urls.py), define the URL patterns:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list, name='post_list'),  # or task_list for Todo App
    path('post/new/', views.post_create, name='post_create'),  # or task_create for Todo App
    path('post/<int:pk>/edit/', views.post_edit, name='post_edit'),  # or task_edit for Todo App
    path('post/<int:pk>/delete/', views.post_delete, name='post_delete'),  # or task_delete for Todo App
]

In mini_blog_project/urls.py, include the blog app URLs:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),  # or todo.urls for Todo App
]

🧩 Step 7: Create Templates

Now, let’s create the HTML templates for listing, creating, and editing posts.

7.1: Post List Template (post_list.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Blog</title>
</head>
<body>
    <h1>Blog Posts</h1>
    <a href="{% url 'post_create' %}">Create a new post</a>
    <ul>
        {% for post in posts %}
            <li>
                <h2>{{ post.title }}</h2>
                <p>{{ post.content }}</p>
                <a href="{% url 'post_edit' post.pk %}">Edit</a>
                <a href="{% url 'post_delete' post.pk %}">Delete</a>
            </li>
        {% endfor %}
    </ul>
</body>
</html>

7.2: Post Form Template (post_form.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create/Edit Post</title>
</head>
<body>
    <h1>{% if form.instance.pk %}Edit{% else %}Create{% endif %} Post</h1>
    <form method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">{% if form.instance.pk %}Save Changes{% else %}Create Post{% endif %}</button>
    </form>
</body>
</html>

🧩 Step 8: Run the App

Finally, run the server and check out your app!

python manage.py runserver

Go to http://127.0.0.1:8000/ in your browser to see your Mini Blog or Todo App in action!


⚠️ **GitHub.com Fallback** ⚠️