Chapter 4. Create CRUD Web Application

In this chapter, we'll explore how to create a basic CRUD (Create, Read, Update, Delete) web application using Django. We'll guide you through key concepts like views, URL dispatchers, templates, and settings to build dynamic web applications that interact with a database.
What We Cover in This Chapter
The following topics are covered in this chapter:
CRUD Web Application
In this section, we’ll introduce the core idea of CRUD, which stands for Create, Read, Update, and Delete. These are the fundamental operations of a web application, allowing users to interact with and modify data stored in a database.
Basic CRUD Structure in Django
This part will show you how to structure a simple CRUD application in Django. You’ll learn how to set up models, views, and templates to handle the four CRUD operations effectively.
Django Generic Views
In this section, we’ll explore Django’s generic views, which simplify the creation of views for common tasks. We'll focus on five key generic views: ListView, DetailView, CreateView, UpdateView, and DeleteView.
How To Write Class-Based Views with Generic Views
This topic will show you how to write class-based views (CBVs) using Django’s generic views. We’ll walk through the process of defining a class-based view, customizing it, and using attributes like model
, template_name
, and context_object_name
.
Generic View Basic Attributes
In this section, we’ll cover the basic attributes of Django’s generic views, such as model
, queryset
, template_name
, and others, to customize the behavior and appearance of your views.
URL Dispatcher for CRUD Views
Learn how Django’s URL dispatcher works to map HTTP requests to the appropriate views. This section will guide you through creating URL patterns for your CRUD views and using path converters like <int:pk>
for dynamic URL matching.
Django Templates for CRUD Views
Explore how Django templates are used to display data for CRUD views. You'll learn how to create templates for list pages, detail pages, and forms for create, update, and delete operations.
Django Template Language (DTL)
This section introduces Django Template Language (DTL), a powerful templating system that allows you to embed dynamic content in your HTML templates. You’ll learn about variables, filters, and template tags that make it easy to generate dynamic content in your views.
Template for List Page
Here, we'll focus on designing a template for the list page, where you can display a list of objects fetched from the database. We’ll discuss how to iterate over a list of objects using the {% for %}
loop in Django templates.
get_FOO_display method
We’ll cover the get_FOO_display
method, which allows you to convert model field choices into human-readable values, making your templates more user-friendly.
Template for Detail Page
This section explains how to create a template for the detail page, where you display information about a specific object from the database. Unlike the list page, the detail page displays a single record.
Template with Model Relations
Learn how to handle model relations in Django templates. This section explains how to display data from related models using the ForeignKey
and ManyToManyField
relationships in your templates.
Template for Create and Update Page
In this section, we will show you how to create templates for the create and update pages, which allow users to submit data using HTML forms. We’ll cover form handling and how to use Django’s form tags to display forms on these pages.
Template for Delete Page
Learn how to create a confirmation page for deleting records in this section. We’ll show you how to create a form for the delete operation and how to display a confirmation message for the user.
Add Links – {% url %} tag
We’ll explain how to use the {% url %}
tag in Django templates to generate links to different views in your application. This tag helps with reverse URL resolution and ensures that URLs are generated dynamically based on the URL name.
Extend Templates – {% extends %} tag
Learn how to create a base template that can be extended by other templates using the {% extends %}
tag. This approach helps in avoiding repetition of common page structures like headers, footers, and navigation bars.
Check Developing App UI on Mobile Device
This section explains how to test your web application’s UI on mobile devices. You’ll learn how to configure Django to make your app accessible on mobile devices using local IP addresses.
Django Templates with Bootstrap
We’ll show you how to integrate Bootstrap with Django templates to create responsive and attractive user interfaces for your CRUD views. Learn how to use Bootstrap’s pre-built components and utilities to enhance your web design.
Crispy Forms
In this section, we’ll explore how to use the Crispy Forms package to style Django forms with Bootstrap, making your form templates more attractive and user-friendly.
Customize Views (1) – Change List Order
We’ll demonstrate how to customize your views to change the order of records displayed on the list page. Learn how to use the order_by
method to sort your querysets based on different fields.
Customize Views (2) – Filter Lists
Learn how to filter lists based on specific criteria using the filter
method. This section shows you how to display filtered data based on user inputs or predefined conditions.
Context
Learn how to pass context data from your views to templates. This section will help you understand how Django’s context system works and how to use it effectively in your views and templates.
Customize Views (3) – Add Extra Context
In this section, we’ll demonstrate how to add extra context to your views by overriding the get_context_data
method. This allows you to pass additional data to your templates, such as the current date or other dynamic information.
Modularize Templates – {% include %} tag
Learn how to modularize your templates by using the {% include %}
tag. This tag allows you to reuse template fragments, such as headers, footers, or sidebars, across multiple templates.
Static Files in Development Environment – {% static %} tag
This section explains how to serve static files, such as images, CSS, and JavaScript, in your Django development environment. We’ll cover the {% static %}
tag and how to link static files in your templates.
STATIC_URL and STATICFILES_DIRS
Learn how to configure static files in Django using the STATIC_URL
and STATICFILES_DIRS
settings. This section will help you manage static files for both development and production environments.
Create Index HTML
In this section, we’ll walk you through the process of creating an index page for your Django project. You’ll learn how to structure the URL patterns and define a view for the home page, making it accessible to users when they visit your site.
Learn offline for better focus!
A book for this course is available on Amazon.
Django Visual Guide
Step-By-Step Manual for Complete Beginners to Mastering Django, Python, and Web Development.
Get the Book Now