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)