Django Introduction

Django is a high-level Python web framework that lets you build secure, database-driven websites quickly.

Django is a free, open-source web framework written in Python. It gives you almost everything you need to build a database-backed website — routing, an ORM, templates, forms, an admin panel and authentication — so you can focus on your app instead of reinventing the basics.

What Django gives you

  • URL routing — map clean URLs to Python code.
  • ORM — talk to your database using Python objects instead of SQL.
  • Templates — render HTML with a safe, simple language.
  • Admin site — a ready-made interface to manage your data.
  • Batteries included — auth, forms, security and sessions out of the box.

Django powers sites like Instagram, Pinterest and Mozilla. It follows the philosophy of being "the web framework for perfectionists with deadlines."

Example

Example · python
# Django is written in Python.
# A view is just a function that returns a response.
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django!")

When to use it

  • A startup uses Django to launch a full-featured e-commerce site in weeks instead of months.
  • A data science team wraps their ML model in a Django API to serve predictions to a React frontend.
  • A newspaper publishes articles through a Django-powered CMS with admin, search, and user comments built in.

More examples

Minimal Django view function

Shows the simplest possible Django view that returns plain text to a browser.

Example · python
from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to Django!")

Render an HTML template

Uses Django's render shortcut to pass data to an HTML template.

Example · python
from django.shortcuts import render

def home(request):
    context = {"site_name": "SoundsCode"}
    return render(request, "home.html", context)

Redirect to a named URL

Demonstrates how Django views can redirect users to another named URL.

Example · python
from django.shortcuts import redirect

def old_home(request):
    return redirect("home")  # named URL pattern

Discussion

  • Be the first to comment on this lesson.