# Django Workshop Day 2

## 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:

```text
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:

```bash
python manage.py startapp blog
```

Django creates:

```text
blog/

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

For now we only need:

```text
views.py
```

* * *

# 12\. Understanding Views

A View is a Python function.

A View decides what the user sees.

Example:

```python
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:

```text
User
 ↓
URL
 ↓
View
 ↓
Response
```

* * *

# 13\. Creating Multiple Views

Open:

```text
blog/views.py
```

Write:

```python
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:

```text
blog/urls.py
```

Write:

```python
from django.urls import path
from .views import *

urlpatterns = [

    path('', home),

    path('about/', about),

    path('contact/', contact),

]
```

Meaning:

```text
/          → home()

/about/    → about()

/contact/  → contact()
```

* * *

# 15\. Connect App URLs

Open:

```text
project/urls.py
```

Write:

```python
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:

```text
Project URL
      ↓
Blog URL
      ↓
View
```

* * *

# 16\. Why Templates?

Suppose we write:

```python
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:

```text
blog/

└── templates/

    └── blog/

        ├── home.html

        ├── about.html

        └── contact.html
```

* * *

# 18\. Creating Home Page

home.html

```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

```python
from django.shortcuts import render

def home(request):

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

What happens?

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

* * *

# 20\. Sending Data to HTML

Instead of hard coding text:

```html
<h1>Welcome Ram</h1>
```

We can send data from Python.

View:

```python
def home(request):

    context = {

        "name":"Ram"

    }

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

HTML:

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

Output:

```text
Welcome Ram
```

* * *

# 21\. Sending Multiple Data

View:

```python
context = {

    "name":"Ram",

    "course":"Django",

    "day":2

}
```

HTML:

```html
<h1>{{ name }}</h1>

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

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

Output:

```text
Ram

Django

Day 2
```

* * *

# 22\. Loops in Templates

Suppose we have students.

View:

```python
context = {

    "students":[

        "Ram",

        "Sita",

        "Hari",

        "Gita"

    ]

}
```

HTML:

```html
<h2>Student List</h2>

{% for student in students %}

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

{% endfor %}
```

Output:

```text
Ram

Sita

Hari

Gita
```

The loop works exactly like Python.

* * *

# 23\. Conditions in Templates

HTML:

```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

```python
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

```python
def home(request):

    context = {

        "blogs":blogs

    }

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

* * *

# Step 3: Display Blogs

home.html

```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:

```text
My Blog Website

Python Basics
Author : Ram

Django Introduction
Author : Sita

HTML Fundamentals
Author : Hari
```

* * *

# Step 4: Add About Page

views.py

```python
def about(request):

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

about.html

```html
<h1>About Us</h1>

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

* * *

# Step 5: Add Navigation

home.html

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

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

<hr>
```

about.html

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

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

<hr>
```

Now users can move between pages.

* * *

# Final Project Structure

```text
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:

```text
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.
