# Day 1 Django Note

## 1\. What is Django?

Django is a Python framework used to build websites and web applications.

It gives us a proper structure to build web apps easily.

With Django, we can build:

*   Blog website
    
*   School system
    
*   Inventory system
    
*   E-commerce website
    
*   Learning platform
    
*   Admin dashboard
    

Django mainly helps us with:

*   URL handling
    
*   Views
    
*   HTML rendering
    
*   Database structure
    
*   Security
    

* * *

## 2\. How Django Works

Django works in a simple flow:

```text
User opens a URL
        ↓
Django checks urls.py
        ↓
Django calls a view function
        ↓
View returns response or HTML page
        ↓
User sees output in browser
```

Simple example:

```text
User opens /
        ↓
urls.py finds matching path
        ↓
home view is called
        ↓
home.html is rendered
```

* * *

## 3\. Django MVT Architecture

Django follows MVT architecture.

*   M = Model
    
*   V = View
    
*   T = Template
    

Simple meaning:

| Component | Purpose |
| --- | --- |
| Model | Database structure |
| View | Python logic |
| Template | HTML page |

For now, just understand:

```text
URL calls View
View renders Template
Model is used to design database table
```

* * *

## 4\. Create Project Folder

```bash
mkdir django_day_1
cd django_day_1
```

* * *

## 5\. Create Virtual Environment

```bash
python -m venv venv
```

Activate virtual environment.

### Windows

```bash
venv\Scripts\activate
```

### Mac/Linux

```bash
source venv/bin/activate
```

* * *

## 6\. Install Django

```bash
pip install django
```

Check version:

```bash
django-admin --version
```

* * *

## 7\. Create Django Project

```bash
django-admin startproject myproject
```

Go inside project:

```bash
cd myproject
```

Run server:

```bash
python manage.py runserver
```

Open in browser:

```text
http://127.0.0.1:8000/
```

You should see Django's default welcome page.

* * *

## 8\. Important Files

Project structure:

```text
myproject/
│
├── manage.py
│
└── myproject/
    ├── settings.py
    ├── urls.py
    ├── asgi.py
    └── wsgi.py
```

### manage.py

Used to run Django commands.

Example:

```bash
python manage.py runserver
```

### settings.py

Contains project settings such as:

*   Installed apps
    
*   Database settings
    
*   Template settings
    
*   Middleware
    

### urls.py

Connects URL with view function.

* * *

## 9\. First Page Using HttpResponse

Open:

```text
myproject/urls.py
```

Write:

```python
from django.contrib import admin
from django.urls import path
from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to Django Day 1")

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", home),
]
```

Open:

```text
http://127.0.0.1:8000/
```

Output:

```text
Welcome to Django Day 1
```

* * *

## 10\. What is HttpResponse?

HttpResponse sends a direct response from Django to the browser.

Example:

```python
return HttpResponse("Hello Django")
```

We can also send simple HTML:

```python
return HttpResponse("<h1>Hello Django</h1>")
```

But for real pages, we use HTML templates.
