Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Django IntroductionChapter 4. Create CRUD Web Application

URL Dispatcher for CRUD Views

URL Dispatcher for CRUD Views

URL Dispatching in Django for CRUD Operations

As we explained in Chapter 2, The URL dispatcher is one of the key concepts in the Django architecture. The URL dispatcher maps HTTP requests to the view functions or classes specified in the URL patterns written in the urls.py file.

In the case on the previous page, as there are five basic CRUD views, you need to prepare five URL patterns. Usually, application-specific URL patterns are written in the urls.py file in each app directory.

1. Two urls.py files

There are two urls.py files you need to handle:

  • Project urls.py: This file is created by the startproject command
  • App urls.py: A new urls.py file under the app directory. This file is not created by the startproject and startapp commands. You need to create it yourself.
URL Dispatcher: Two urls.py files

2. Create and Edit App urls.py

As the app urls.py file is not automatically created, you need to create it first. Then, edit the file with the guidance below.

Import views

First, you need to import the necessary modules. You need to import all views from views.py.

Django: Import views

Add the URL patterns

You need to add one URL pattern for each view. There are three arguments when you set URL patterns.

Django: Add URL patterns

1. URL

In this part, you can set a URL you want to use. To avoid confusion, use a slug related to the view. Usually, the kebab-case is used for this part. You can set a variable in a part of the URL using path converter <>, which will be explained later.

2. View name

In this part, you can specify which view you want to link to the URL. For class-based views, usually, PascalCase is used. You need to add .as_view() after the view name for class-based views. You don't need to add .as_view() when you use function-based views.

3. URL name

In Chapter 2, we only covered the first two arguments but not the name argument. The name argument is useful when you handle the reverse URL resolution. For example, by setting this, you can call the URL in views.py.

Path converter – <init:pk>

Using a path converter, you can set a variable in the URL. The variable is defined using angle brackets <>. The most frequently used one is <int:pk>. The int part is a data type. int means zero or any positive integer. The pk part is a variable. pk means primary key, which is set in the auto-generated id field by default. <int:pk> is just an example. You can set other types of path converters using the <data type:name> format. The data type examples are:

  • str - Matches any non-empty string
  • slug - Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters

3. Edit the project urls.py file

Creating and editing the urls.py file under the app directory is not enough to complete the URL dispatcher. You need to link the newly created urls.py file with the project urls.py file by adding the include function, as in the example below.

Django: Edit Project urls.py

Practice

In this practice, we'll create a URL dispatcher for the views designed in the previous practice.

1. Create a new urls.py file under the employee_learning directory

The urls.py file is not generated by the startapp command. You need to create a new urls.py file under the employee_learning directory.

Command Line - INPUT
cd employee_learning
touch urls.py

2. Edit the app urls.py

a. Import views

As the views.py and urls.py files are in the same directory, use . (dot) before views. The following five views need to be imported.

  • CourseList
  • CourseDetail
  • CourseCreate
  • CourseUpdate
  • CourseDelete

The yellow line below is the new code.

employee_learning/urls.py
from django.urls import path
from .views import CourseList, CourseDetail, CourseCreate, CourseUpdate, CourseDelete

b. Add URL patterns for list and create pages

URL patterns are simple as you don't need to specify which data records will be displayed for the list and create pages.

employee_learning/urls.py
from django.urls import path
from .views import CourseList, CourseDetail, CourseCreate, CourseUpdate, CourseDelete

urlpatterns = [
    path('course-list/', CourseList.as_view(), name='course_list'),
    path('course-create/', CourseCreate.as_view(), name='course_create'),
]

c. Add URL patterns for detail, update and delete pages

You need to call specific data records for the detail, update, and delete pages. To call a specific data record, add <int:pk> in the URL.

employee_learning/urls.py
 : 
urlpatterns = [
    path('course-list/', CourseList.as_view(), name='course_list'),
    path('course-detail/<int:pk>/', CourseDetail.as_view(), name='course_detail'),
    path('course-create/', CourseCreate.as_view(), name='course_create'),
    path('course-update/<int:pk>/', CourseUpdate.as_view(), name='course_update'),
    path('course-delete/<int:pk>/', CourseDelete.as_view(), name='course_delete'),
]

3. Add an urlpattern in the project urls.py

To connect paths between the two urls.py, add a urlpattern in the main urls.py under the config directory.

config/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('test-app/', include('test_app.urls')),
    path('employee-learning/', include('employee_learning.urls')),
]

Django documentation reference: URL dispatcher


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Understanding CRUD in Web Applications

CRUD Web Application

Understanding CRUD in Web Applications

CRUD Web Application

Tags:

URL dispatcher

urls.py

CRUD

Reverse URL Resolution

Path Converter

Django Introduction
Course Content

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment