0% found this document useful (0 votes)
4 views12 pages

TS-08 Django Views

Uploaded by

Nazmul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

TS-08 Django Views

Uploaded by

Nazmul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Django Views

Function-based and
Class-based views

Program: EDGE-CSE CUET DIGITAL Instructor:


SKILLS TRAINING Tanveer Rahman,
Sr. Software Engineer,
Course: Python (Django) [Intermediate]
Friends Corp.
Lecture: Training Session-08 Chuo-ku, Tokyo, JP
Date: 8th October, 2024 [email protected],
Duration: (1+1) hours 01626735005
Views handle
web requests and
return responses.
Django Two main types
Views of views in
Django:
Function-based views
(FBVs)
• Class-based views
Definition

• FBVs are Python functions that


take web requests and return
Function- responses.
• Use decorators to handle
Example:
Based permissions, authentication, etc.

Views
(FBVs) Advantages:

Simple and easy to implement.


• Ideal for small, straightforward
logic.
Task 1: Create a Function-
Based View (15 Mins)
Create a basic FBV to display "Welcome to Django!" on
the homepage.
Definition

• CBVs provide a
Class- structured way to handle
views by using Python
Based classes.
• Leverages Object-
Views Oriented Programming to
Example:
(CBVs) reduce code duplication.
Common Class-Based Views
TemplateView: Renders HTML templates.

ListView: Displays a list of objects.

DetailView: Shows detailed view of a single object.

CreateView, UpdateView, DeleteView: Handle form


submissions for creating, updating, and deleting objects.
CBV Advantages

More DRY (Don't Easier to scale and Built-in mixins for


Repeat Yourself) reuse logic with common
compared to FBVs. inheritance. functionality.
Task 2: Create a Class-
Based View(15 Mins)
Create a CBV that displays a list of items from the
database using ListView.
FBVs vs. CBVs
Feature Function-Based Views Class-Based Views

Easier to write for small


Simplicity More structured, scalable
logic

Reusability Harder to reuse code Promotes code reuse

Minimal, relies on
Built-in Functionality Offers many built-in mixins
decorators
Task 3: Refactor FBV to CBV
(15 Mins)
Refactor the FBV from Task 1 into a CBV.
Both FBVs and CBVs
have their own strengths.
Conclusio
n Use FBVs for simple
logic, and CBVs for
scalable, reusable views.
Q&A
Session

You might also like