SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie Rapid Web Development with Python/Django

Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics PresentationShrinath Shenoy
 
Company Visitor Management System Report.docx
Company Visitor Management System Report.docxCompany Visitor Management System Report.docx
Company Visitor Management System Report.docxfantabulous2024
 
An Introduction to Django Web Framework
An Introduction to Django Web FrameworkAn Introduction to Django Web Framework
An Introduction to Django Web FrameworkDavid Gibbons
 
Akash rajguru project report sem v
Akash rajguru project report sem vAkash rajguru project report sem v
Akash rajguru project report sem vAkash Rajguru
 
بررسی چارچوب جنگو
بررسی چارچوب جنگوبررسی چارچوب جنگو
بررسی چارچوب جنگوrailsbootcamp
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoKnoldus Inc.
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoAhmed Salama
 
Django 1.10.3 Getting started
Django 1.10.3 Getting startedDjango 1.10.3 Getting started
Django 1.10.3 Getting startedMoniaJ
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJoaquim Rocha
 
Database Website on Django
Database Website on DjangoDatabase Website on Django
Database Website on DjangoHamdaAnees
 
Django Workflow and Architecture
Django Workflow and ArchitectureDjango Workflow and Architecture
Django Workflow and ArchitectureAndolasoft Inc
 
Django Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersDjango Framework Overview forNon-Python Developers
Django Framework Overview forNon-Python DevelopersRosario Renga
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to djangoIlian Iliev
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django frameworkKnoldus Inc.
 

Ähnlich wie Rapid Web Development with Python/Django (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
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django framework
 

Kürzlich hochgeladen

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Rapid Web Development with Python/Django

  • 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