DEV Community

Gopal Ghate
Gopal Ghate

Posted on

πŸš€ Getting Started with Django – A Beginner-Friendly Guide

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

Enter fullscreen mode Exit fullscreen mode

Check if it’s installed:

django-admin --version # Checking django version
Enter fullscreen mode Exit fullscreen mode

πŸ— 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

Enter fullscreen mode Exit fullscreen mode
  • 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

Enter fullscreen mode Exit fullscreen mode
  • 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")),
]
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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)