Skip to main content

Day 1 Django Note

Updated 3 min readMarkdown

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:

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:

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:

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

4. Create Project Folder

mkdir django_day_1
cd django_day_1

5. Create Virtual Environment

python -m venv venv

Activate virtual environment.

Windows

venv\Scripts\activate

Mac/Linux

source venv/bin/activate

6. Install Django

pip install django

Check version:

django-admin --version

7. Create Django Project

django-admin startproject myproject

Go inside project:

cd myproject

Run server:

python manage.py runserver

Open in browser:

http://127.0.0.1:8000/

You should see Django's default welcome page.


8. Important Files

Project structure:

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

manage.py

Used to run Django commands.

Example:

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:

myproject/urls.py

Write:

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:

http://127.0.0.1:8000/

Output:

Welcome to Django Day 1

10. What is HttpResponse?

HttpResponse sends a direct response from Django to the browser.

Example:

return HttpResponse("Hello Django")

We can also send simple HTML:

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

But for real pages, we use HTML templates.

57 views

No comments yet. Be the first to comment.