SlideShare a Scribd company logo
Python Django
Web Development
Chapter Two
BY:Eng Mohamed Ahmed Mohamed
Function vs. Class Views
 Django allows two styles of views – functions or
class based views
 Functions – take a request object as the first
parameter and must return a response object
 Class based views – allow CRUD operations with
minimal code. Can inherit from multiple generic
view classes (i.e. Mixins)
Sample – As Class Based
View
from .models import Question
from django.views.generic import ListView
class QuestionList(ListView):
model = Question
context_object_name = ‘questions’
urls.py
 Defines routes to send urls to various views
 Can use regular expressions
 Extract parameters from a url and pass to the view as a
named parameter:
r(‘^question/(?P<question_id>d+)/$’,’views.question_detail’)
 Extensible – urls.py can include additional url files from
apps:
r(‘^question/’,include(question.urls))
Views
Working with User Input
Major Steps:
1. Adding an HTML form to our template.
2. Editing the application URLconf.
3. Adding a new view function that processes
user input.
Request & Response
 Request object encapsulate the request and provide access to a
number of attributes and methods for accessing cookies, sessions,
the logged in user object, meta data (i.e environment variables),
 Response objects are returned to the browser. Can set content
type, content length, response does not have to return HTML or a
rendered template
 Special response types allow for common functionality:
HttpResponeRedirect
Http404
HttpStreamingResponse
Quick CRUD Operations with
Generic Views
 ListView
 UpdateView
 CreateView
 If Model is specified, automagically creates a
matching ModelForm
 Form will save the Model if data passes validation
 Override form_valid() method to provide custom
logic (i.e sending email or setting additional fields)
Debugging Hints
The command line you use to run the development server often shows helpful
error messages
Django and the development server support hot-swap; you usually do not need to
restart the server when you change the code. Hot swap works much better on
the development server than in most servers that claim to support it. However,
it doesn’t work fif you change url mappings (see below). Also, note that your
browser may cache responses, so you may need to reload several times to see
changes.
Django Extras
 CRSF Middleware – enabled by default. Include template
tag in all forms:
{%csrf_token%}
 Authentication
 Caching
 Sessions
 Messages
 Email
 Logging
Cross-Site Request Forgery
Django comes with a data-preserving feature
that disallows POSTs which are not secure
against cross-site request forgery (CSRF)
attacks.
You can read more about CSRF at the following
website:
https://docs.djangoproject.com/en/dev/ref/
contrib/csrf/
Cross-Site Request Forgery
For our simple application, two fixes:
1. Add a CSFR token ({% csrf_token %} to forms
that POST back to your site
2. Send the request context instance to the token
via the template.
Thank You
Question
And
Answers

More Related Content

PPTX
Tango with django
PDF
Django class based views
PPTX
Django Frequently Asked Interview Questions
PDF
django_introduction20141030
PDF
Rapid web application development using django - Part (1)
PDF
Introduction to Django
PPTX
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
PPTX
CHAPTER_4_DJANGO_FORMS[1] [Read-Only].pptx
Tango with django
Django class based views
Django Frequently Asked Interview Questions
django_introduction20141030
Rapid web application development using django - Part (1)
Introduction to Django
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
CHAPTER_4_DJANGO_FORMS[1] [Read-Only].pptx

Similar to chapter Two Django basics of dynamic web pages.pptx (20)

PDF
PPTX
Web development with django - Basics Presentation
DOCX
Akash rajguru project report sem v
PPTX
Django framework
KEY
Jumpstart Django
PPTX
Django crush course
DOC
Top Django Interview Questions and Answers in 2025.doc
PPT
Django Forms: Best Practices, Tips, Tricks
PDF
Django Documentation
PDF
The Django Book - Chapter 7 forms
PDF
You've done the Django Tutorial, what next?
DOCX
What is Full Stack with Django and how to start learning It.docx
PDF
GDG Addis - An Introduction to Django and App Engine
ODP
Introduction to Django
PPTX
Django Framework Interview Question and Answer partOne.pptx
PPTX
Django course
PDF
A Basic Django Introduction
PDF
The Django Web Framework (EuroPython 2006)
PDF
Ch9 .Best Practices for Class-Based Views
PDF
Web development with django - Basics Presentation
Akash rajguru project report sem v
Django framework
Jumpstart Django
Django crush course
Top Django Interview Questions and Answers in 2025.doc
Django Forms: Best Practices, Tips, Tricks
Django Documentation
The Django Book - Chapter 7 forms
You've done the Django Tutorial, what next?
What is Full Stack with Django and how to start learning It.docx
GDG Addis - An Introduction to Django and App Engine
Introduction to Django
Django Framework Interview Question and Answer partOne.pptx
Django course
A Basic Django Introduction
The Django Web Framework (EuroPython 2006)
Ch9 .Best Practices for Class-Based Views
Ad

More from bestboybulshaawi (12)

PPTX
Chapter 2 Opensource & Linux _______.pptx
PPTX
Chapter 1 Evolution of Linux___________.pptx
PPTX
Chapter 1 Database Systems______________.pptx
PPTX
CH.1 Artificial intelligencennnnnnnnnnnnnnnnn.pptx
PPTX
CH.3 Machine Learning and AI--------.pptx
PPTX
chapter One Introduction to python django.pptx
PPT
Chapter Three Django Templates System.ppt
PPTX
softwareasaservice-1703220855100000.pptx
PPTX
T02___________Basic_Linux_Commands__pptx
PPTX
672888521-COMPLETE-SAAS-PRESENTATION.pptx
PPTX
Chapter four AI and modern world part two.pptx
PPTX
learnpythondjangochapteroneintroduction.pptx
Chapter 2 Opensource & Linux _______.pptx
Chapter 1 Evolution of Linux___________.pptx
Chapter 1 Database Systems______________.pptx
CH.1 Artificial intelligencennnnnnnnnnnnnnnnn.pptx
CH.3 Machine Learning and AI--------.pptx
chapter One Introduction to python django.pptx
Chapter Three Django Templates System.ppt
softwareasaservice-1703220855100000.pptx
T02___________Basic_Linux_Commands__pptx
672888521-COMPLETE-SAAS-PRESENTATION.pptx
Chapter four AI and modern world part two.pptx
learnpythondjangochapteroneintroduction.pptx
Ad

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Review of recent advances in non-invasive hemoglobin estimation
MIND Revenue Release Quarter 2 2025 Press Release
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
20250228 LYD VKU AI Blended-Learning.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectroscopy.pptx food analysis technology

chapter Two Django basics of dynamic web pages.pptx

  • 1. Python Django Web Development Chapter Two BY:Eng Mohamed Ahmed Mohamed
  • 2. Function vs. Class Views  Django allows two styles of views – functions or class based views  Functions – take a request object as the first parameter and must return a response object  Class based views – allow CRUD operations with minimal code. Can inherit from multiple generic view classes (i.e. Mixins)
  • 3. Sample – As Class Based View from .models import Question from django.views.generic import ListView class QuestionList(ListView): model = Question context_object_name = ‘questions’
  • 4. urls.py  Defines routes to send urls to various views  Can use regular expressions  Extract parameters from a url and pass to the view as a named parameter: r(‘^question/(?P<question_id>d+)/$’,’views.question_detail’)  Extensible – urls.py can include additional url files from apps: r(‘^question/’,include(question.urls))
  • 6. Working with User Input Major Steps: 1. Adding an HTML form to our template. 2. Editing the application URLconf. 3. Adding a new view function that processes user input.
  • 7. Request & Response  Request object encapsulate the request and provide access to a number of attributes and methods for accessing cookies, sessions, the logged in user object, meta data (i.e environment variables),  Response objects are returned to the browser. Can set content type, content length, response does not have to return HTML or a rendered template  Special response types allow for common functionality: HttpResponeRedirect Http404 HttpStreamingResponse
  • 8. Quick CRUD Operations with Generic Views  ListView  UpdateView  CreateView  If Model is specified, automagically creates a matching ModelForm  Form will save the Model if data passes validation  Override form_valid() method to provide custom logic (i.e sending email or setting additional fields)
  • 9. Debugging Hints The command line you use to run the development server often shows helpful error messages Django and the development server support hot-swap; you usually do not need to restart the server when you change the code. Hot swap works much better on the development server than in most servers that claim to support it. However, it doesn’t work fif you change url mappings (see below). Also, note that your browser may cache responses, so you may need to reload several times to see changes.
  • 10. Django Extras  CRSF Middleware – enabled by default. Include template tag in all forms: {%csrf_token%}  Authentication  Caching  Sessions  Messages  Email  Logging
  • 11. Cross-Site Request Forgery Django comes with a data-preserving feature that disallows POSTs which are not secure against cross-site request forgery (CSRF) attacks. You can read more about CSRF at the following website: https://docs.djangoproject.com/en/dev/ref/ contrib/csrf/
  • 12. Cross-Site Request Forgery For our simple application, two fixes: 1. Add a CSFR token ({% csrf_token %} to forms that POST back to your site 2. Send the request context instance to the token via the template.