Django Complete Beginner Guide
Django Overview → Installation → Project → App → Folder Structure → Database → Views → Static Files → Forms → Deployment
What is Django?
Django is a high-level Python web framework used to build:
- Websites
- Web applications
- APIs
- E-commerce systems
- School management systems
- ERP systems
- Social media platforms
- Blogs
Django follows:
MVT Architecture
which stands for:
Model
View
Template
Why Django?
Django provides many built-in features.
Without Django:
You manually create routing
You manually connect databases
You manually create authentication
You manually manage sessions
With Django:
Authentication included
Admin panel included
ORM included
Security included
Forms included
Popular Websites Built Using Django
Examples:
- Instagram (initially)
- Mozilla
- Disqus
Django Features
Fast Development
Many things are already built.
Example:
Login System
Admin Panel
Database Handling
Secure
Built-in protection against:
SQL Injection
CSRF
XSS
Clickjacking
Scalable
Can handle:
Small blog
Large enterprise application
ORM Support
ORM means:
Object Relational Mapper
Instead of SQL:
SELECT * FROM students;
You can write:
Student.objects.all()
Django Architecture (MVT)
Model
Responsible for:
Database Structure
Example:
class Student(models.Model):
name = models.CharField(max_length=100)
View
Handles business logic.
Example:
def home(request):
return HttpResponse("Hello")
Template
Responsible for UI.
Example:
<h1>Hello User</h1>
MVT Flow
User
|
v
URL
|
v
View
|
v
Model
|
v
Database
View
|
v
Template
|
v
Browser
Django Installation
Step 1: Check Python
python --version
or
python3 --version
Step 2: Create Virtual Environment
Why?
Separate project dependencies
Avoid package conflicts
Create:
python -m venv venv
Step 3: Activate Virtual Environment
Windows:
venv\Scripts\activate
Linux/Mac:
source venv/bin/activate
Step 4: Install Django
pip install django
Verify Installation
django-admin --version
Example:
5.x.x
Creating a Django Project
Create Project
Syntax:
django-admin startproject myproject
Example:
django-admin startproject school
Project Structure
school/
|
|-- manage.py
|
|-- school/
|
|-- __init__.py
|-- settings.py
|-- urls.py
|-- asgi.py
|-- wsgi.py
Enter Project
cd school
Run Server
python manage.py runserver
Output:
Starting development server...
Open:
http://127.0.0.1:8000
Understanding manage.py
Main command center.
Examples:
python manage.py runserver
python manage.py migrate
python manage.py createsuperuser
python manage.py makemigrations
Understanding settings.py
Project configuration file.
Contains:
Installed Apps
Database Settings
Templates
Static Files
Middleware
Security Settings
Understanding urls.py
Handles routing.
Example:
from django.urls import path
urlpatterns = [
]
Understanding wsgi.py
Used by:
Production Web Servers
Example:
Gunicorn
uWSGI
Understanding asgi.py
Used for:
WebSockets
Async Requests
Channels
Creating an Application
What is an App?
A Django project can contain multiple apps.
Example:
School Project
students app
teachers app
accounts app
fees app
Create App
python manage.py startapp students
App Structure
students/
|
|-- admin.py
|-- apps.py
|-- models.py
|-- views.py
|-- tests.py
|-- migrations/
Register App
Open:
settings.py
Add:
INSTALLED_APPS = [
...
'students',
]
Understanding App Files
models.py
Database tables.
Example:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
views.py
Business logic.
Example:
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome")
admin.py
Admin registration.
Example:
from django.contrib import admin
from .models import Student
admin.site.register(Student)
migrations/
Stores database changes.
Example:
0001_initial.py
Database in Django
Default database:
SQLite
Database file:
db.sqlite3
Configure Database
Inside:
settings.py
Default:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Create Model
Example:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
Make Migration
python manage.py makemigrations
Apply Migration
python manage.py migrate
ORM Example
Create:
Student.objects.create(
name="John",
age=20
)
Read:
Student.objects.all()
Filter:
Student.objects.filter(age=20)
Delete:
student.delete()
Views in Django
A view receives:
Request
and returns:
Response
Example:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello Django")
URL Mapping
app urls.py
from django.urls import path
from .views import home
urlpatterns = [
path('', home)
]
project urls.py
from django.urls import include, path
urlpatterns = [
path('', include('students.urls'))
]
Templates
Create:
templates/
Example:
<h1>Welcome</h1>
View:
from django.shortcuts import render
def home(request):
return render(
request,
"home.html"
)
Static Files
Static files are:
CSS
JavaScript
Images
Fonts
Structure
static/
|
|-- css/
|-- js/
|-- images/
settings.py
STATIC_URL = 'static/'
Template Usage
{% load static %}
<link rel="stylesheet"
href="{% static 'css/style.css' %}">
Forms in Django
Forms collect user input.
Example:
<form method="POST">
<input type="text" name="name">
</form>
Django Form
forms.py
from django import forms
class StudentForm(forms.Form):
name = forms.CharField()
View
from .forms import StudentForm
def student_form(request):
form = StudentForm()
return render(
request,
"form.html",
{"form": form}
)
Form Template
<form method="POST">
{% csrf_token %}
{{ form }}
<button>Submit</button>
</form>
CSRF Token
Important:
{% csrf_token %}
Protects against:
CSRF Attacks
Django Admin Panel
Create admin user:
python manage.py createsuperuser
Run:
python manage.py runserver
Open:
http://127.0.0.1:8000/admin
Common Beginner Mistakes
Mistake 1
Not activating virtual environment.
Wrong:
pip install django
inside global environment.
Mistake 2
Forgetting app registration.
Wrong:
students app created
but not added in:
INSTALLED_APPS
Mistake 3
Not running migrations.
Wrong:
Model created
but:
makemigrations
migrate
not executed.
Mistake 4
Forgetting include()
Wrong:
path('', students.urls)
Correct:
path('', include('students.urls'))
Mistake 5
Forgetting csrf token.
Wrong:
<form method="POST">
Correct:
{% csrf_token %}
Mistake 6
Editing database manually.
Use:
makemigrations
migrate
Mistake 7
Putting business logic in templates.
Keep logic inside:
Views
Models
Django Project Deployment
Deployment means:
Making website available online
Development Server vs Production
Development:
python manage.py runserver
Production:
Gunicorn
Nginx
Apache
uWSGI
Popular Deployment Options
VPS
Examples:
Ubuntu Server
DigitalOcean
AWS EC2
Linode
Vultr
Cloud Platforms
Examples:
Railway
Render
PythonAnywhere
Heroku-like platforms
Typical Production Stack
User
|
v
Nginx
|
v
Gunicorn
|
v
Django
|
v
PostgreSQL
Production Checklist
✅ DEBUG = False
DEBUG = False
✅ Allowed Hosts
ALLOWED_HOSTS = [
"example.com"
]
✅ Use PostgreSQL
Better than:
SQLite
for production.
✅ Collect Static Files
python manage.py collectstatic
✅ Secure Secret Key
Never expose:
SECRET_KEY
✅ HTTPS
Use SSL certificate.
Real Project Structure
school/
|
|-- manage.py
|
|-- school/
|
|-- students/
|
|-- teachers/
|
|-- templates/
|
|-- static/
|
|-- media/
|
|-- requirements.txt
|
|-- db.sqlite3
Mini Homework
Q1
What does Django stand for?
Q2
What is MVT architecture?
Q3
Create a Django project named:
college
Q4
Create an app named:
students
Q5
Which command starts the server?
Q6
Which file stores database models?
Q7
What is ORM?
Q8
Why do we use migrations?
Q9
What are static files?
Q10
Why is DEBUG=False important in production?
Homework Solutions
A1
Django is a Python web framework.
A2
Model
View
Template
A3
django-admin startproject college
A4
python manage.py startapp students
A5
python manage.py runserver
A6
models.py
A7
Object Relational Mapper.
A8
To apply database schema changes.
A9
CSS
JS
Images
Fonts
A10
Protects sensitive production information.
Quick Revision
| Topic | Purpose |
|---|---|
| Project | Entire website |
| App | Specific feature |
| Model | Database |
| View | Logic |
| Template | UI |
| URL | Routing |
| ORM | Database access |
| Static | CSS/JS/Images |
| Form | User Input |
| Migration | Database changes |
| Admin | Management Panel |
Summary
In this chapter you learned:
- Django Overview
- Django Installation
- Creating Projects
- Using manage.py
- Project Structure
- Creating Applications
- Folder Structure
- Models and Databases
- Views and URLs
- Templates
- Static Files
- Forms
- Admin Panel
- Deployment Basics
This is the foundation of Django. The next chapters should cover:
- Django Models Deep Dive
- All Django Field Types
- CRUD Operations
- Templates and Template Tags
- Django Forms Deep Dive
- Authentication and Authorization
- Class Based Views
- Generic Views
- Django ORM Advanced Queries
- Django REST Framework (DRF)
- Production Deployment Project