Introduction to Django - potatoscript/django GitHub Wiki

๐ŸŽ“ What is Django?

๐Ÿง  Think of Django as a robot friend that helps you build websites quickly and safely.
๐Ÿ’ก It handles boring things like login, databases, and pages โ€” so you can focus on making cool features.


๐Ÿ—๏ธ Django is a Framework

A framework is like a Lego instruction book ๐Ÿงฑ
It shows you how to build your project, step-by-step, with tools and shortcuts.

๐Ÿงฉ Without Django ๐Ÿงฐ With Django
You build everything from scratch. Django gives you built-in blocks (admin panel, login system, database)
You write your own security code. Django protects you by default.
It takes a long time. You build websites super fast.

โœจ What Can Django Do?

โœ… Build Blogs ๐Ÿ“
โœ… Build Todo Lists ๐Ÿงพ
โœ… Make Online Stores ๐Ÿ›’
โœ… Create Login Systems ๐Ÿ”
โœ… Show Maps, Charts, and Dashboards ๐Ÿ“Š๐Ÿ—บ๏ธ


โš™๏ธ How Django Works โ€“ Big Picture

Letโ€™s imagine Django is a pizza shop ๐Ÿ•

  1. User places an order ๐Ÿง‘โ€๐Ÿณ (User opens your website and types a link)
  2. Django reads the order ๐Ÿ“„ (URL is matched with a view)
  3. Chef (view) prepares it ๐Ÿณ (Python function creates a response)
  4. Plate (template) styles it ๐Ÿฝ๏ธ (HTML + CSS formats it)
  5. Pizza (webpage) is served ๐Ÿ• (Page is sent back to the user)

๐ŸŽจ Here's a simple diagram:

Browser โžก๏ธ URL โžก๏ธ View โžก๏ธ Template โžก๏ธ Browser

๐Ÿš€ Why Use Django?

โค๏ธ Benefit ๐ŸŽฏ Description
๐Ÿ” Secure Prevents hacking with built-in safety tools
โšก Fast You can build a web app in days
๐Ÿ”Œ Full-featured Login system, admin dashboard, and database all included
๐Ÿ“š Well-documented Tons of tutorials and help online
๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ Big community Many developers use Django worldwide

๐Ÿ“ฆ Prerequisites (What You Need)

  • โœ… Python 3 installed ๐Ÿ
  • โœ… A Code Editor (like VS Code) ๐Ÿ’ป
  • โœ… Internet connection ๐ŸŒ
  • โœ… Willing to learn and play with code ๐Ÿง ๐ŸŽฎ

๐Ÿ› ๏ธ Step-by-Step: Installing Django

Letโ€™s build your first Django project together! ๐ŸŽ‰


๐ŸŒฑ Step 1: Install Python

๐Ÿ–ฅ๏ธ Go to https://www.python.org/downloads
Download the latest version and install it.

โœ… Check it's working:

python --version

๐Ÿงช Step 2: Create a Virtual Environment

A virtual environment is like a tiny Python world for your project ๐ŸŒ

python -m venv myenv

Activate it:

  • ๐ŸชŸ Windows:
    myenv\Scripts\activate
    
  • ๐Ÿ Mac/Linux:
    source myenv/bin/activate
    

๐Ÿ“ฆ Step 3: Install Django

pip install django

โœ… Check Django installed:

django-admin --version

๐Ÿงฑ Step 4: Create a New Project

Letโ€™s name our project "mysite" ๐Ÿ—๏ธ

django-admin startproject mysite
cd mysite

Inside youโ€™ll see:

File What it does
manage.py Your magic remote to control Django ๐Ÿ”ฎ
settings.py Where you set your website rules ๐Ÿ”ง
urls.py Map website links to views ๐ŸŒ

๐Ÿ”ฅ Step 5: Run the Development Server

python manage.py runserver

๐Ÿ“ก Now open your browser and go to:

http://127.0.0.1:8000/

๐ŸŽ‰ Youโ€™ll see Djangoโ€™s โ€œIt worked!โ€ rocket page ๐Ÿš€


๐Ÿ“‹ Step 6: Make Your First App

Apps are like little machines inside Django โš™๏ธ

python manage.py startapp hello

Your folder structure grows:

mysite/
โ”œโ”€โ”€ hello/
โ”‚   โ”œโ”€โ”€ views.py โ† you write what to show here
โ”‚   โ””โ”€โ”€ urls.py  โ† (you make this file soon)

๐Ÿง  Step 7: Create Your First Page

Inside hello/views.py:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello world! ๐ŸŒ")

Then make hello/urls.py:

from django.urls import path
from . import views

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

Now connect it to the main urls.py in mysite/urls.py:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('hello.urls')),  # ๐Ÿ‘ˆ Connect the app!
]

โœ… Refresh the browser โ†’ Youโ€™ll see "Hello world! ๐ŸŒ"