Showing posts with label Beginner Django Guide. Show all posts
Showing posts with label Beginner Django Guide. Show all posts

Build a Blog and To-Do App in Django: Full Step-by-Step Guide with Code (2025)

← Back to Home

 

๐Ÿงฑ Build a Blog and To-Do List App in Django – Step-by-Step (With Code)


Here's a step-by-step Django tutorial to help you:

  1. ✅ Build a Blog App

  2. ✅ Build a To-Do List App

  3. ✅ Combine both into a single Django project with two separate apps

๐Ÿงฐ Prerequisites

  • Python installed

  • Django installed:

    pip install django
    
  • Basic knowledge of Python and HTML


1️⃣ Create the Django Project

django-admin startproject mysite
cd mysite

2️⃣ Create the Blog App

python manage.py startapp blog

➕ Register Blog App in mysite/settings.py

INSTALLED_APPS = [
    ...
    'blog',
]

๐Ÿงพ blog/models.py

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)

    def __str__(self):
        return self.title

๐Ÿ” Run Migrations

python manage.py makemigrations
python manage.py migrate

๐Ÿงพ blog/views.py

from django.shortcuts import render
from .models import Post

def post_list(request):
    posts = Post.objects.all().order_by('-created_at')
    return render(request, 'blog/post_list.html', {'posts': posts})

๐Ÿงพ blog/urls.py

from django.urls import path
from . import views

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

๐Ÿงพ mysite/urls.py (Edit)

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

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

๐Ÿ–ผ️ blog/templates/blog/post_list.html

<!DOCTYPE html>
<html>
<head><title>My Blog</title></head>
<body>
    <h1>Blog Posts</h1>
    {% for post in posts %}
        <h2>{{ post.title }}</h2>
        <p>{{ post.content }}</p>
        <small>{{ post.created_at }}</small><hr>
    {% empty %}
        <p>No posts yet.</p>
    {% endfor %}
</body>
</html>

Test it:

python manage.py runserver

Visit: http://127.0.0.1:8000/blog/


3️⃣ Create the To-Do App

python manage.py startapp todo

➕ Add 'todo' to INSTALLED_APPS


๐Ÿงพ todo/models.py

from django.db import models

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

    def __str__(self):
        return self.title
python manage.py makemigrations
python manage.py migrate

๐Ÿงพ todo/views.py

from django.shortcuts import render, redirect
from .models import Task

def task_list(request):
    tasks = Task.objects.all()
    return render(request, 'todo/task_list.html', {'tasks': tasks})

def add_task(request):
    if request.method == 'POST':
        title = request.POST['title']
        Task.objects.create(title=title)
    return redirect('task_list')

def toggle_task(request, task_id):
    task = Task.objects.get(id=task_id)
    task.completed = not task.completed
    task.save()
    return redirect('task_list')

๐Ÿงพ todo/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.task_list, name='task_list'),
    path('add/', views.add_task, name='add_task'),
    path('toggle/<int:task_id>/', views.toggle_task, name='toggle_task'),
]

๐Ÿงพ mysite/urls.py (Add to existing)

path('todo/', include('todo.urls')),

๐Ÿ–ผ️ todo/templates/todo/task_list.html

<!DOCTYPE html>
<html>
<head><title>To-Do List</title></head>
<body>
    <h1>My To-Do List</h1>
    <form method="post" action="{% url 'add_task' %}">
        {% csrf_token %}
        <input type="text" name="title" placeholder="New task">
        <button type="submit">Add</button>
    </form>
    <ul>
        {% for task in tasks %}
            <li>
                <a href="{% url 'toggle_task' task.id %}">
                    {% if task.completed %}
                        <s>{{ task.title }}</s>
                    {% else %}
                        {{ task.title }}
                    {% endif %}
                </a>
            </li>
        {% empty %}
            <li>No tasks yet!</li>
        {% endfor %}
    </ul>
</body>
</html>

Test:
Visit http://127.0.0.1:8000/todo/


๐Ÿงฉ 4️⃣ Combine Both Apps in One Project

You already did it — both blog and todo are separate apps inside one project, accessible via:

  • http://127.0.0.1:8000/blog/

  • http://127.0.0.1:8000/todo/

Each app:

  • Has its own models, views, templates, and URL routes

  • Can be developed independently

  • Shares the same database and project settings


๐ŸŽ Bonus: Add Navigation Links

Add this nav to both templates:

<a href="/blog/">Blog</a> | <a href="/todo/">To-Do</a>

๐Ÿ Conclusion

You’ve built:

  • ✅ A full-featured Blog app

  • ✅ A functional To-Do List app

  • ✅ A combined Django project that includes both

This modular approach mirrors how real-world Django sites are structured — scalable and organized.



Django Made Simple: Complete Beginner-to-Pro Guide to Build Web Apps (2025)

← Back to Home

 

๐Ÿš€ Django Made Simple: The Complete Beginner-to-Pro Guide (2025)


๐Ÿง  What is Django?

Django is a high-level Python web framework that allows you to build secure, scalable, and maintainable web applications quickly.

✅ Motto: “The web framework for perfectionists with deadlines.”

It comes with built-in tools for:

  • User authentication

  • Database handling

  • Admin panels

  • URL routing

  • Template rendering


๐ŸŽฏ Why Use Django?

Feature Benefit
๐Ÿงฉ Modular Reusable code (apps)
๐Ÿ” Secure Protects against common attacks
๐Ÿš€ Fast Built-in admin, ORM, routing
๐Ÿ“ฆ Batteries Included Everything is included: No need to install many libraries
๐ŸŒ Scalable Used by Instagram, Pinterest, Spotify

๐Ÿ› ️ How Django Works (Visual Breakdown)

๐Ÿ–ผ️ Image idea: A diagram showing the flow → Browser → URL → View → Model → Template → Response

  • URLConf: Maps URLs to views

  • Views: Handle logic and return responses

  • Models: Define your data structure (database)

  • Templates: Control how HTML is displayed


๐Ÿ“ฆ Setting Up Django

๐Ÿ”ง Step 1: Install Django

pip install django

๐Ÿ”ง Step 2: Create a Django Project

django-admin startproject mysite
cd mysite

๐Ÿ”ง Step 3: Run the Server

python manage.py runserver

๐Ÿ”— Visit http://127.0.0.1:8000/ in your browser — you’ll see the Django welcome screen!


๐Ÿงฑ Creating a Simple Django App

Let’s create a blog app.

▶ Step 1: Start the App

python manage.py startapp blog

▶ Step 2: Register the App

Open mysite/settings.py and add 'blog' to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'blog',
]

๐Ÿ—‚️ Add Your First View

๐Ÿงพ blog/views.py

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django Blog!")

๐Ÿ”— Map the URL

๐Ÿงพ blog/urls.py (Create this file)

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
]

๐Ÿงพ mysite/urls.py (Edit this)

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

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

Now visit http://127.0.0.1:8000/ again → You’ll see: "Hello, Django Blog!"


๐Ÿ’พ Connecting to a Database

Django uses SQLite by default, but you can connect it to PostgreSQL, MySQL, etc.

๐Ÿงพ blog/models.py

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)

    def __str__(self):
        return self.title

๐Ÿ› ️ Run Migrations

python manage.py makemigrations
python manage.py migrate

๐Ÿ“‹ Create and Use the Admin Panel

๐Ÿ‘ค Create Admin User

python manage.py createsuperuser

Login at http://127.0.0.1:8000/admin/ with your credentials.

๐Ÿงพ blog/admin.py

from django.contrib import admin
from .models import Post

admin.site.register(Post)

๐Ÿ–ผ️ Image idea: Screenshot of Django admin panel showing blog posts.


๐Ÿง‘‍๐Ÿ’ป Real-World Django Project Examples

๐Ÿ“ฐ 1. Blog Website

  • Use: Create, edit, and publish posts.

  • Tech: Django + SQLite + Bootstrap

๐Ÿ›’ 2. E-commerce Store

  • Use: Product listings, cart, checkout.

  • Tech: Django + Stripe API + PostgreSQL

๐Ÿ“… 3. Event Management App

  • Use: Host and RSVP events.

  • Tech: Django + FullCalendar + REST API


๐Ÿš€ Tips for SEO & Performance in Django

Tip How
✅ Clean URLs         Use slug fields in URLs
✅ Meta Tags         Use template blocks to inject meta
✅ Sitemap         Use django.contrib.sitemaps
✅ Page Speed         Use WhiteNoise, compress images, cache views

๐Ÿ“š Django Resources


๐Ÿ Conclusion

Django is shortcut to building modern web apps with less code and more structure. Whether you’re a beginner learning your first framework or a professional creating scalable platforms, Django has the tools you need — and then some.

๐Ÿง  Next Step:  building a blog or to-do list app using this guide!



Featured Post

Number Guessing Game (code) in python

← Back to Projects About the project: This is a simple number guessing game and it is suitable for beginners who are learning python progra...

Popular Posts