BudiBadu Logo
Samplebadu
Django

Learn Django by Examples

Django 5.0+

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It is free and open source.

This page provides comprehensive code samples to help learners understand Django's ORM and model system. From basic field types to advanced relationship mappings and query optimizations, each example includes complete code snippets with detailed explanations to master database interactions in Django.

Model Field Types

Django models are the single, definitive source of information about your data. They contain the essential fields and behaviors of the data you're storing. This code example demonstrates the most common field types used to map Python attributes to database columns.

Model Default Values

Setting default values for model fields is a common requirement. This sample code shows how Django allows you to specify static values or callable functions to generate defaults dynamically when a new object is created.

Model Relationship Mapping

Relational databases are powerful because they relate tables to each other. Django offers a robust API to define these relationships using ForeignKey, OneToOneField, and ManyToManyField. This code example focuses on the fundamental Many-to-One relationship.

Foreign Key Constraints

Maintaining database integrity is crucial when deleting referenced data. This sample code demonstrates how Django provides several `on_delete` behaviors to control what happens to child records when a parent record is deleted.

One To One Mapping

A One-to-One relationship is essentially a ForeignKey with a unique constraint. This code example shows how it is commonly used to extend existing models (like the built-in User model) with additional information.

Many To Many Mapping

Many-to-Many relationships occur when multiple records in one table are associated with multiple records in another. This sample code shows how Django handles the intermediate "join table" automatically, or allows you to define a custom one.

Model Query Expressions

Django's F() and Q() objects allow for complex database queries. This sample code shows how F() objects reference model fields directly in the database, while Q() objects enable complex lookups with OR and AND logic.

Session Data Storage

Sessions allow you to store arbitrary data about the current site visitor across requests. This code example shows how Django handles the details of generating session IDs and storing the data securely.

Cookie Data Handling

Cookies are small pieces of data stored in the user's browser. This example shows how to set, retrieve, and delete cookies securely, including signed cookies that cannot be tampered with.

User Permission Rules

Django has a built-in permission system. This example shows how to check permissions in views using decorators and mixins, and how to check them in templates.

User Authentication

How to manually log a user in and out. While Django provides built-in views for this, this code example shows how understanding the underlying authenticate() and login() functions is essential for custom flows.

Password Hashing

Django never stores passwords in plain text. It uses the PBKDF2 algorithm with a SHA256 hash by default. This example shows how to manually hash passwords and check them, which is useful for custom user creation flows.

Message Framework Usage

The messages framework allows you to temporarily store messages in one request and retrieve them for display in a subsequent request. This sample code is perfect for "Item saved!" notifications.

Signal Receiver Functions

Signals allow decoupled applications to get notified when certain actions occur elsewhere in the framework. This code example shows a common use case: creating a UserProfile automatically when a new User is registered.

Middleware Request Hooks

Middleware is a framework of hooks into Django's request/response processing. This sample code demonstrates a light, low-level "plugin" system for globally altering Django's input or output.