Skip to main content

Django Workshop Day 2

Updated 5 min readMarkdown

Building a Simple Blog Website

What We Will Learn Today

  1. Creating Django Apps

  2. Understanding Views

  3. URL Routing

  4. Templates

  5. render()

  6. Sending Data to Templates

  7. Loops

  8. Conditions

  9. Building a Simple Blog Website


11. What is a Django App?

Think of a project as a school.

Inside the school there are many departments.

Example:

School Website

├── Student App
├── Teacher App
├── Attendance App
├── Blog App

Each part is called an App.

Today we will create a Blog App.

Create app:

python manage.py startapp blog

Django creates:

blog/

├── admin.py
├── apps.py
├── models.py
├── views.py
├── tests.py
└── migrations/

For now we only need:

views.py

12. Understanding Views

A View is a Python function.

A View decides what the user sees.

Example:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to my Blog")

When a user visits a page, Django calls a View.

Simple Flow:

User
 ↓
URL
 ↓
View
 ↓
Response

13. Creating Multiple Views

Open:

blog/views.py

Write:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Home Page")

def about(request):
    return HttpResponse("About Page")

def contact(request):
    return HttpResponse("Contact Page")

Now we have three pages.


14. URL Routing

URL routing tells Django:

"When a user visits this URL, call this View."

Create:

blog/urls.py

Write:

from django.urls import path
from .views import *

urlpatterns = [

    path('', home),

    path('about/', about),

    path('contact/', contact),

]

Meaning:

/          → home()

/about/    → about()

/contact/  → contact()

15. Connect App URLs

Open:

project/urls.py

Write:

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

urlpatterns = [

    path('admin/', admin.site.urls),

    path('', include('blog.urls')),

]

Now Django can access our app URLs.

Flow:

Project URL
      ↓
Blog URL
      ↓
View

16. Why Templates?

Suppose we write:

return HttpResponse("<h1>Home Page</h1>")

This works.

But imagine a website with:

  • 500 lines of HTML

  • CSS

  • Images

  • Navigation Bar

Writing everything inside Python becomes difficult.

So Django uses Templates.

Template = HTML File


17. Creating Templates Folder

Create:

blog/

└── templates/

    └── blog/

        ├── home.html

        ├── about.html

        └── contact.html

18. Creating Home Page

home.html

<!DOCTYPE html>

<html>

<head>
    <title>Home</title>
</head>

<body>

    <h1>My Blog Website</h1>

    <p>Welcome to Django Workshop</p>

</body>

</html>

19. Using render()

Open views.py

from django.shortcuts import render

def home(request):

    return render(
        request,
        "blog/home.html"
    )

What happens?

User opens URL
       ↓
View executes
       ↓
render() finds home.html
       ↓
Browser shows page

20. Sending Data to HTML

Instead of hard coding text:

<h1>Welcome Ram</h1>

We can send data from Python.

View:

def home(request):

    context = {

        "name":"Ram"

    }

    return render(
        request,
        "blog/home.html",
        context
    )

HTML:

<h1>Welcome {{ name }}</h1>

Output:

Welcome Ram

21. Sending Multiple Data

View:

context = {

    "name":"Ram",

    "course":"Django",

    "day":2

}

HTML:

<h1>{{ name }}</h1>

<p>{{ course }}</p>

<p>Day {{ day }}</p>

Output:

Ram

Django

Day 2

22. Loops in Templates

Suppose we have students.

View:

context = {

    "students":[

        "Ram",

        "Sita",

        "Hari",

        "Gita"

    ]

}

HTML:

<h2>Student List</h2>

{% for student in students %}

    <p>{{ student }}</p>

{% endfor %}

Output:

Ram

Sita

Hari

Gita

The loop works exactly like Python.


23. Conditions in Templates

HTML:

{% if students %}

    <h2>Students Available</h2>

{% else %}

    <h2>No Students Available</h2>

{% endif %}

Django checks the condition and displays the result.


24. Mini Project - Simple Blog Website

Now let's build a complete Blog Website.

Instead of using a database, we will store blogs inside a Python list.

This keeps things simple.


Step 1: Create Blog Data

views.py

from django.shortcuts import render

blogs = [

    {
        "title":"Python Basics",
        "author":"Ram"
    },

    {
        "title":"Django Introduction",
        "author":"Sita"
    },

    {
        "title":"HTML Fundamentals",
        "author":"Hari"
    }

]

Step 2: Send Data to Template

def home(request):

    context = {

        "blogs":blogs

    }

    return render(
        request,
        "blog/home.html",
        context
    )

Step 3: Display Blogs

home.html

<!DOCTYPE html>

<html>

<head>
    <title>Blog Website</title>
</head>

<body>

<h1>My Blog Website</h1>

{% for blog in blogs %}

    <h2>{{ blog.title }}</h2>

    <p>Author : {{ blog.author }}</p>

    <hr>

{% endfor %}

</body>

</html>

Output:

My Blog Website

Python Basics
Author : Ram

Django Introduction
Author : Sita

HTML Fundamentals
Author : Hari

Step 4: Add About Page

views.py

def about(request):

    return render(
        request,
        "blog/about.html"
    )

about.html

<h1>About Us</h1>

<p>
This website was created using Django.
</p>

Step 5: Add Navigation

home.html

<a href="/">Home</a>

<a href="/about/">About</a>

<hr>

about.html

<a href="/">Home</a>

<a href="/about/">About</a>

<hr>

Now users can move between pages.


Final Project Structure

project/

├── manage.py

├── blog/

│   ├── views.py

│   ├── urls.py

│   └── templates/

│       └── blog/

│           ├── home.html

│           └── about.html

Day 2 Summary

Today we learned:

  1. Django Apps

  2. Views

  3. URLs

  4. Templates

  5. render()

  6. Context

  7. Variables

  8. Loops

  9. Conditions

  10. Creating Multiple Pages

  11. Building a Simple Blog Website

Complete Flow:

User
 ↓
URL
 ↓
View
 ↓
Context Data
 ↓
Template
 ↓
Browser

Tomorrow (Day 3) we will connect our Blog Website to a real database using Models and Admin Panel.

34 views

No comments yet. Be the first to comment.