Skip to content
dreamcode
dreamcode
Map
Django overview
Lesson 70 of 77
+15 XP on finish
PYTHON APPLIEDChapter 12 · Python for Web Development

Django: the batteries-included framework

Django is a full-featured Python web framework that follows the Model-View-Template pattern. It comes with a built-in ORM for database queries, an admin panel, authentication, and URL routing out of the box.

Worked example
# models.py - define your data shape
from django.db import models

class Star(models.Model):
    name = models.CharField(max_length=100)
    magnitude = models.FloatField()

    def __str__(self):
        return self.name

# views.py - handle HTTP requests
from django.http import JsonResponse
from .models import Star

def star_list(request):
    stars = list(Star.objects.values("name", "magnitude"))
    return JsonResponse(stars, safe=False)

How it reads

  • models.Model turns a Python class into a database table via the ORM
  • Star.objects.values(...) queries the database and returns matching rows
  • JsonResponse sends data back to the client as JSON
Cloud tip: Django's ORM lets you query the database using Python instead of writing raw SQL. Migrations keep your database schema in sync with your models.

Check your understanding

0 / 3

Answer all 3 to complete this lesson and earn 15 XP.

  1. 1. What design pattern does Django follow?
  2. 2. What does Django's ORM allow you to do?
  3. 3. Which command starts the Django development server?
Answer every question to unlock the next lesson.