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 ๐
- User places an order ๐งโ๐ณ (User opens your website and types a link)
- Django reads the order ๐ (URL is matched with a view)
- Chef (view) prepares it ๐ณ (Python function creates a response)
- Plate (template) styles it ๐ฝ๏ธ (HTML + CSS formats it)
- 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! ๐"