SlideShare a Scribd company logo
Rapid Web Application
Development using Python/
Django framework
Part 1
<made by: Nishant Soni>
WHY PYTHON ?
Python is a general-purpose interpreted, interactive, object-
oriented, and high-level programming language. It was created
by Guido van Rossum during 1985- 1990. Like Perl, Python
source code is also available under the GNU General Public
License (GPL).
1
2
3
4
5
I T I S H I G H L Y R E A D A B L E .
I T I S I N T E R A C T I V E .
I T I S O B J E C T O R I E N T E D .
I T I S S C A L A B L E .
I T I S E X T E N D A B L E .
Applications of Python
1
Web & Internet
Development
2
Scientific & Numeric
computing
3
Building Desktop
GUI's
4
Software
Development
5
Business Application
Development
made by Nishant Soni
Django is a high-level Python web framework that
encourages rapid development and clean progmatic
design.
Some Features of Django Framework:
> ORM
> Automatic Admin Interface
> Cache Infrasturcture
> Internationalization
> Templating System
> Command Line job framework
> Regex based URL Design
INTRODUCTION TO
DJANGO
"For Perfectionists with
deadlines"
SOUDERTON SENIOR HIGH SCHOOmade by Nishant Soni
MODEL (models.py) Defines the data structure and handles the database queries. It defines the
data in python and iteracts with it). Typically contained in RDBMS(MySQL,
SQLite etc.) other data storage mechanisms are also possible as well (LDAP
etc.)
Views (views.py)
TEMPLATE
Determines what data from the model should be returned in HTTP
response. Tasks involve reading and writing to the database. (usually in
views.py)
Renders data in HTML(or JSON or XML) and makes it look pretty
There is also a Controller (URL dispatcher)- this maps the requested urlto a
view function and calls it . If caching is enabled , the view function can
check to see if a cached version of the page exists and bypass all the further
steps , returning cached version. (urls.py)
CONTROLLER
DJANGO ARCHITECTURE
MVT - "Model-View-Template"
made by Nishant Soni
DJANGO
ARCHITECTURE
(cont'd)
Django HTTPs Request
Handler
Django Flow goes something like this:
1) Visitor’s browser asks for a URL.
2) Django matches the request against its urls.py files.
3) If a match is found, Django moves on to the view that’s
associated with the URL. Views are generally found inside each app
in the views.py file.
4) The view generally handles all the database manipulation. It
grabs data and passes it on.
5) A template (specified in the view) then displays that data.
Django
Project
Layout
<PROJECT_ROOT>
manage.py
<PROJECT_DIR>
__init__.py
settings.py
urls.py
wsgi.py
SOUDERTON SENIOR HIGH SCHOOL
settings.py is a core file in Django projects. It holds all the
configuration values that your web app needs to work; database
settings, logging configuration, where to find static files, API keys if
you work with external APIs, and a bunch of other stuff.
Settings.py contains :
> Core Settings
> Auth
> Messages
> Sessions
> Sites
> Static Files
> Core Settings Topical Index
Django- settings.py
Defines the settings used by our Django
Application
made by Nishant Soni
Sample
Settings.py
An Django App is an submodule of the project which
is a self-sufficient. An app typically has it's own
models.py.
DJANGO- APPS
Apps bring modularity to your project.! 
made by Nishant Soni
Django App
Layout
Django Model contains the essential fields and
behaviors of the data you're storing. Generally, each
model maps to a single database table. The basics:
Each model is a Python class that subclasses
django.db.models.Model .
Django - Models
"let the models be fatter, and views thin"
made by Nishant Soni
Django
Models
Example
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from datetime import datetime,date
from taggit.managers import TaggableManager
from ckeditor.fields import RichTextField
from home.models import Category ,Author
from django.template.defaultfilters import slugify
# Create your models here
class Blogs(models.Model):
title=models.CharField(max_length=50,null=False,blank=False)
slug = models.SlugField(blank=True)
content=RichTextField()
tags=TaggableManager()
categories=models.ForeignKey(Category, related_name='comments')
author=models.ForeignKey(Author)
published_on=models.DateField(default=datetime.now(),blank=True)
image=models.ImageField(upload_to='images/blogs/')
def __str__(self):
return self.title
class Meta:
verbose_name_plural="blogs"
Activating
Django
Models
> Add the app to INSTALLED_APPS
in settings.py
> Run manage.py validate
> Run manage.py syncdb
> Migrations
> Write custom script or manually
handle migrations
Use South
Django Views take an HTTP Request and return an HTTP Response to the user.
Any Python callable can be a view.The only hard and fast requirement is that it
takes the request object (customarily named request) as its first argument.
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("Hello, World")
Django - views.py
made by Nishant Soni
1. Django allows two styles of views – functions or class based views.
2. Functions – take a request object as the first parameter and must
return a response object.
3. Class based views – allow CRUD operations with minimal code. 
Can inherit from multiple generic view classes (i.e. Mixins)
Django - views.py (cont'd)
made by Nishant Soni
Function Based View is very easy to implement and it’s
very useful but the main disadvantage is that on a large
Django project, there are usually a lot of similar
functions in the views; one case could be that all objects
of a Django project usually have CRUD operations, so
this code is repeated again and again unnecessarily, and
so, this was one of the reasons that the class-based
views. Function-based generic views are created to
address the common use cases.
Example of Function based view in following slide:
Django - Function
based view
made by Nishant Soni
Django views.py
(function based view)
Django ships with generic views to do the following:
>    Display list and detail pages for a single object. If we were creating an
application to manage conferences then a TalkListView and a
RegisteredUserListView would be examples of list views. A single talk page
is an example of what we call a “detail” view.
>    Present date-based objects in year/month/day archive pages, associated
detail, and “latest” pages.
>    Allow users to create, update, and delete objects – with or without
authorization.
Django - Generic Views or
class based view
Generic View take certain common idioms and patterns found
in view development and abstract them so that you can
quickly write common views of data without having to write
too much code.
made by Nishant Soni
The simplest way to use generic views is to create them directly in your URLconf. If you’re
only changing a few simple attributes on a class-based view, you can simply pass them into
the as_view() method call itself:
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
    path('about/', TemplateView.as_view(template_name="about.html")),
]
The second, more powerful way to use generic views is to inherit from an existing view and
override attributes (such as the template_name) or methods (such as get_context_data) in
your subclass to provide new values or methods. (As shown in following slide)
Django - Generic Views or class based
view (cont'd)
Django provides an example of some classes which can be used as views.
These allow you to structure your views and reuse code by harnessing
inheritance and mixins
made by Nishant Soni
Django views.py
(class based view)
To design URLs for an app, you create a Python module informally
called a URLconf (URL configuration). This module is pure Python
code and is a mapping between URL path expressions to Python
functions (your views).
Django - Urls.py
made by Nishant Soni
Sample
Django
urls.py
> Preparing and restructuring data to make it ready for
rendering
> Creating HTML forms for the data
> Receiving and processing submitted forms and data
from the client
Django - forms
Django handles three distinct parts of the work involved in
forms:
made by Nishant Soni
Django - ModelForms
Our Teachers and Assistant Teachers
A ModelForm maps a model class’s fields to HTML form
<input> elements via a Form; this is what the Django admin
is based upon.
made by Nishant Soni
How to use
a
Model Form
Instantiating, processing, and rendering forms
1. When rendering an object in Django, we generally:
2. Get hold of it in the view (fetch it from the database,
for example)
3. Pass it to the template context
4. Expand it to HTML markup using template variables
Made with love by :
Nishant Soni
IT Engineer | Python Developer
contact: nishantsoni78@gmail.com
www.linkedin.com/in/thenishantsoni

More Related Content

Similar to Rapid web application development using django - Part (1)

Django by rj
Django by rjDjango by rj
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
Shrinath Shenoy
 
Company Visitor Management System Report.docx
Company Visitor Management System Report.docxCompany Visitor Management System Report.docx
Company Visitor Management System Report.docx
fantabulous2024
 
Basic Python Django
Basic Python DjangoBasic Python Django
Basic Python Django
Kaleem Ullah Mangrio
 
An Introduction to Django Web Framework
An Introduction to Django Web FrameworkAn Introduction to Django Web Framework
An Introduction to Django Web Framework
David Gibbons
 
Django Introdcution
Django IntrodcutionDjango Introdcution
Django Introdcution
Nagi Annapureddy
 
React django
React djangoReact django
React django
Heber Silva
 
Akash rajguru project report sem v
Akash rajguru project report sem vAkash rajguru project report sem v
Akash rajguru project report sem v
Akash Rajguru
 
بررسی چارچوب جنگو
بررسی چارچوب جنگوبررسی چارچوب جنگو
بررسی چارچوب جنگو
railsbootcamp
 
Django
DjangoDjango
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
Knoldus Inc.
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
Ahmed Salama
 
Django 1.10.3 Getting started
Django 1.10.3 Getting startedDjango 1.10.3 Getting started
Django 1.10.3 Getting started
MoniaJ
 
Django
DjangoDjango
Django
sisibeibei
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
Joaquim Rocha
 
Database Website on Django
Database Website on DjangoDatabase Website on Django
Database Website on Django
HamdaAnees
 
Django Workflow and Architecture
Django Workflow and ArchitectureDjango Workflow and Architecture
Django Workflow and Architecture
Andolasoft Inc
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python Developers
Rosario Renga
 
Django Framework Interview Guide - Part 1
Django Framework Interview Guide - Part 1Django Framework Interview Guide - Part 1
Django Framework Interview Guide - Part 1
To Sum It Up
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
Ilian Iliev
 

Similar to Rapid web application development using django - Part (1) (20)

Django by rj
Django by rjDjango by rj
Django by rj
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
 
Company Visitor Management System Report.docx
Company Visitor Management System Report.docxCompany Visitor Management System Report.docx
Company Visitor Management System Report.docx
 
Basic Python Django
Basic Python DjangoBasic Python Django
Basic Python Django
 
An Introduction to Django Web Framework
An Introduction to Django Web FrameworkAn Introduction to Django Web Framework
An Introduction to Django Web Framework
 
Django Introdcution
Django IntrodcutionDjango Introdcution
Django Introdcution
 
React django
React djangoReact django
React django
 
Akash rajguru project report sem v
Akash rajguru project report sem vAkash rajguru project report sem v
Akash rajguru project report sem v
 
بررسی چارچوب جنگو
بررسی چارچوب جنگوبررسی چارچوب جنگو
بررسی چارچوب جنگو
 
Django
DjangoDjango
Django
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Django 1.10.3 Getting started
Django 1.10.3 Getting startedDjango 1.10.3 Getting started
Django 1.10.3 Getting started
 
Django
DjangoDjango
Django
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Database Website on Django
Database Website on DjangoDatabase Website on Django
Database Website on Django
 
Django Workflow and Architecture
Django Workflow and ArchitectureDjango Workflow and Architecture
Django Workflow and Architecture
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python Developers
 
Django Framework Interview Guide - Part 1
Django Framework Interview Guide - Part 1Django Framework Interview Guide - Part 1
Django Framework Interview Guide - Part 1
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
 

Recently uploaded

Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
GDSC PJATK
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
flufftailshop
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 

Recently uploaded (20)

Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 

Rapid web application development using django - Part (1)

  • 1. Rapid Web Application Development using Python/ Django framework Part 1 <made by: Nishant Soni>
  • 2. WHY PYTHON ? Python is a general-purpose interpreted, interactive, object- oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). 1 2 3 4 5 I T I S H I G H L Y R E A D A B L E . I T I S I N T E R A C T I V E . I T I S O B J E C T O R I E N T E D . I T I S S C A L A B L E . I T I S E X T E N D A B L E .
  • 3. Applications of Python 1 Web & Internet Development 2 Scientific & Numeric computing 3 Building Desktop GUI's 4 Software Development 5 Business Application Development made by Nishant Soni
  • 4. Django is a high-level Python web framework that encourages rapid development and clean progmatic design. Some Features of Django Framework: > ORM > Automatic Admin Interface > Cache Infrasturcture > Internationalization > Templating System > Command Line job framework > Regex based URL Design INTRODUCTION TO DJANGO "For Perfectionists with deadlines" SOUDERTON SENIOR HIGH SCHOOmade by Nishant Soni
  • 5. MODEL (models.py) Defines the data structure and handles the database queries. It defines the data in python and iteracts with it). Typically contained in RDBMS(MySQL, SQLite etc.) other data storage mechanisms are also possible as well (LDAP etc.) Views (views.py) TEMPLATE Determines what data from the model should be returned in HTTP response. Tasks involve reading and writing to the database. (usually in views.py) Renders data in HTML(or JSON or XML) and makes it look pretty There is also a Controller (URL dispatcher)- this maps the requested urlto a view function and calls it . If caching is enabled , the view function can check to see if a cached version of the page exists and bypass all the further steps , returning cached version. (urls.py) CONTROLLER DJANGO ARCHITECTURE MVT - "Model-View-Template" made by Nishant Soni
  • 7. Django HTTPs Request Handler Django Flow goes something like this: 1) Visitor’s browser asks for a URL. 2) Django matches the request against its urls.py files. 3) If a match is found, Django moves on to the view that’s associated with the URL. Views are generally found inside each app in the views.py file. 4) The view generally handles all the database manipulation. It grabs data and passes it on. 5) A template (specified in the view) then displays that data.
  • 8.
  • 10. SOUDERTON SENIOR HIGH SCHOOL settings.py is a core file in Django projects. It holds all the configuration values that your web app needs to work; database settings, logging configuration, where to find static files, API keys if you work with external APIs, and a bunch of other stuff. Settings.py contains : > Core Settings > Auth > Messages > Sessions > Sites > Static Files > Core Settings Topical Index Django- settings.py Defines the settings used by our Django Application made by Nishant Soni
  • 12. An Django App is an submodule of the project which is a self-sufficient. An app typically has it's own models.py. DJANGO- APPS Apps bring modularity to your project.!  made by Nishant Soni
  • 14. Django Model contains the essential fields and behaviors of the data you're storing. Generally, each model maps to a single database table. The basics: Each model is a Python class that subclasses django.db.models.Model . Django - Models "let the models be fatter, and views thin" made by Nishant Soni
  • 15. Django Models Example # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models from datetime import datetime,date from taggit.managers import TaggableManager from ckeditor.fields import RichTextField from home.models import Category ,Author from django.template.defaultfilters import slugify # Create your models here class Blogs(models.Model): title=models.CharField(max_length=50,null=False,blank=False) slug = models.SlugField(blank=True) content=RichTextField() tags=TaggableManager() categories=models.ForeignKey(Category, related_name='comments') author=models.ForeignKey(Author) published_on=models.DateField(default=datetime.now(),blank=True) image=models.ImageField(upload_to='images/blogs/') def __str__(self): return self.title class Meta: verbose_name_plural="blogs"
  • 16. Activating Django Models > Add the app to INSTALLED_APPS in settings.py > Run manage.py validate > Run manage.py syncdb > Migrations > Write custom script or manually handle migrations Use South
  • 17. Django Views take an HTTP Request and return an HTTP Response to the user. Any Python callable can be a view.The only hard and fast requirement is that it takes the request object (customarily named request) as its first argument. from django.http import HttpResponse def hello_world(request): return HttpResponse("Hello, World") Django - views.py made by Nishant Soni
  • 18. 1. Django allows two styles of views – functions or class based views. 2. Functions – take a request object as the first parameter and must return a response object. 3. Class based views – allow CRUD operations with minimal code.  Can inherit from multiple generic view classes (i.e. Mixins) Django - views.py (cont'd) made by Nishant Soni
  • 19. Function Based View is very easy to implement and it’s very useful but the main disadvantage is that on a large Django project, there are usually a lot of similar functions in the views; one case could be that all objects of a Django project usually have CRUD operations, so this code is repeated again and again unnecessarily, and so, this was one of the reasons that the class-based views. Function-based generic views are created to address the common use cases. Example of Function based view in following slide: Django - Function based view made by Nishant Soni
  • 21. Django ships with generic views to do the following: >    Display list and detail pages for a single object. If we were creating an application to manage conferences then a TalkListView and a RegisteredUserListView would be examples of list views. A single talk page is an example of what we call a “detail” view. >    Present date-based objects in year/month/day archive pages, associated detail, and “latest” pages. >    Allow users to create, update, and delete objects – with or without authorization. Django - Generic Views or class based view Generic View take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to write too much code. made by Nishant Soni
  • 22. The simplest way to use generic views is to create them directly in your URLconf. If you’re only changing a few simple attributes on a class-based view, you can simply pass them into the as_view() method call itself: from django.urls import path from django.views.generic import TemplateView urlpatterns = [     path('about/', TemplateView.as_view(template_name="about.html")), ] The second, more powerful way to use generic views is to inherit from an existing view and override attributes (such as the template_name) or methods (such as get_context_data) in your subclass to provide new values or methods. (As shown in following slide) Django - Generic Views or class based view (cont'd) Django provides an example of some classes which can be used as views. These allow you to structure your views and reuse code by harnessing inheritance and mixins made by Nishant Soni
  • 24. To design URLs for an app, you create a Python module informally called a URLconf (URL configuration). This module is pure Python code and is a mapping between URL path expressions to Python functions (your views). Django - Urls.py made by Nishant Soni
  • 26. > Preparing and restructuring data to make it ready for rendering > Creating HTML forms for the data > Receiving and processing submitted forms and data from the client Django - forms Django handles three distinct parts of the work involved in forms: made by Nishant Soni
  • 27.
  • 28. Django - ModelForms Our Teachers and Assistant Teachers A ModelForm maps a model class’s fields to HTML form <input> elements via a Form; this is what the Django admin is based upon. made by Nishant Soni
  • 29. How to use a Model Form Instantiating, processing, and rendering forms 1. When rendering an object in Django, we generally: 2. Get hold of it in the view (fetch it from the database, for example) 3. Pass it to the template context 4. Expand it to HTML markup using template variables
  • 30. Made with love by : Nishant Soni IT Engineer | Python Developer contact: nishantsoni78@gmail.com www.linkedin.com/in/thenishantsoni