If youโve ever wondered how to build powerful, scalable web applications without reinventing the wheel, Django is your best friend. Django is a high-level Python web framework that allows you to build web apps quickly, securely, and with less code. In this article, weโll walk through the basics of Django, its project structure, and get you ready to write your first app.
๐ฏ Why Django?
Django is popular because it follows the "batteries-included" philosophy, meaning it comes with everything you need to build web apps:
โ ORM (Object-Relational Mapper): Work with databases using Python instead of writing SQL.
โ Admin Panel: Auto-generated backend to manage your data.
โ Security: Protection against common vulnerabilities (SQL injection, CSRF, XSS).
โ Scalability: Used by Instagram, Pinterest, and Disqus.
In short โ Django makes web development fast, secure, and fun.
๐ Installing Django
python3 -m venv venv # creates virtual environment for project
source venv/bin/activate # activate virtual environment
pip install django # installing django
Check if itโs installed:
django-admin --version # Checking django version
๐ Creating Your First Project
django-admin startproject mysite
cd mysite
# your folder structure should look like this.
mysite/
โโโ manage.py
โโโ mysite/
โ โโโ __init__.py
โ โโโ settings.py
โ โโโ urls.py
โ โโโ asgi.py
โ โโโ wsgi.py
- 1๏ธโฃ manage.py โ Your Projectโs Remote Control This file is used to run commands related to your project.
python manage.py runserver # Start development server
python manage.py startapp blog # Create a new app
python manage.py makemigrations # Look at models and creates migration files that describe those changes.
python manage.py migrate # Apply database changes
- 2๏ธโฃ settings.py This file contains all the configuration settings:
Database setup: Which database youโre using (default: SQLite).
Installed apps: Which Django apps are active.
Security keys: Secret key for cryptography, allowed hosts.
Static files: CSS, JavaScript, images.
You can think of it as your projectโs control center.
- 3๏ธโฃ urls.py - routing This file decides which view handles which URL.
from django.urls import path
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome Home!")
urlpatterns = [
path('', home),
path('about/', lambda r: HttpResponse("About Page")),
]
- 4๏ธโฃ wsgi.py & asgi.py โ The Gatekeepers
These files allow your project to talk to web servers in production:
WSGI: For traditional synchronous servers like Gunicorn.
ASGI: For asynchronous servers like Daphne, great for WebSockets.
You rarely modify these files, but theyโre essential for deployment.
- โถ Running the Development Server
python manage.py runserver
Open your browser at http://127.0.0.1:8000/. ๐ You just ran your first Django project!
๐ Key Takeaways
Django is a full-stack framework with built-in tools.
Your project structure has specific files for config, routing, and deployment.
manage.py is your main command-line tool.
urls.py maps URLs to views.
Top comments (0)