Django
Python
Basic of Django
Main Topic
we will discuss these Topic
Object realtion mapper
URLs Views
Template
Forms
Authentication
Admin
Internationalization
Security
More Deatils
Extra Deatils are
Model
View
Controller
Database
Session
Cookies
Ajax
Services
Basic Commands
start app
django-admin startproject nameOfProject
Run Server
Python manage.py runserver
Run server on local machine
Object Realtion Mapper
we can modal data entierly in Python and we get rich Database API for free we can write SQL if needed
we can modal dataentierly in Python and we get rich Database API for free we can write SQL if needed
we can modal dataentierly in Python and we get rich Database API for free we can write SQL if needed
URL and Views
A clean URL schema is important for app Django Provids a god way
we define schime or URLs in URL config where we simply map out all the app where your will be re-dirted etc
Template
It is simaply the HTML Pages
use for front end develpor and is flexible we can also use the Python logic in the template
Same as simple HTML
Forms
A poweful way the we can render the HTML and appply the Logic in Python
Mostaly used in Input fields to handel data user can creat and upload data
import forms
Authentication
It comes along with the authenications and sessions and permissons to the user
make easy for user that can Login and og out in the app
we can secure pages also
from django.contrib.auth.decorators import login_required
Admin
It provide automatic and production ready admin Panel
Admin can easily manage the content of the site
Provides many Hooks for the customization
from django.contrib import admin
Internationalization
It provide us the method that we can change the language on the base of time zone
It is up to developers that what part of app should be translates in what time zone
from django.utils.translation import gettext
Security
It provids a lot of security featues like
SQL injection
Remote code Execution
Cross SIte Request Forgery
Cross site script
Clickjacking
Additional fetaurs
Folder structure
init_.py : Just for pyhton treat this folder as package
setting.py : is used for the project setting of Projexct
urls : include all URLs of the app and Links on click of functions
Creat a App
we can create appp in side Django Projects
Like Big app having a large services and featurs as in App
Command is : " python manage.py startapp nameofApp" it will creat another folder in that
Admin Interface
provide ready to use UI for Admin Panel
Make sure You have install all depencies foe admin panel check out at setting .py file
we need to initize the Database
python manage.py mirgant creatsuperuser
Then we need to add it to URLs file url(r'^admin/', include(admin.site.urls)),
Views
simply they are Python functions that take request and response to the request
Response can be simply the HTML file or Documnet or Image
we can also render HTML pages in the response
def hello(request):
return render(request, "myapp/template/hello.html", {})
Model View Controller
It is Made up of follwing three Patterns
Model : Lowest form that is responsible for mantaining the data
View : is response for Displaying the data
Controller : software that interact between Model and view
Model
it is responsible for the managimg the Data of the application
It response to the request from the view
it is also responsible to instructions from controller to update itself
View
it Means Presenting data In particular form by controller decision to present data
They are script base template for the PHP ASP and very easy to inregrate with AJAX technolgy
Controller
It is responsible for responde the user input and and perform interaction on data model view
It recive input then validate data and perform Bussiness operations on it
Urls
It is used for mapping and is done in URL.py file. When a request is made in a App it look in URL .py file if it is present then it will renser the file other wise 404 page
from django.conf.urls import patterns, include, url
Template
It provides us the way that we can write and cal pythin code in the HTML file and use then seprately It take three argumnets
Render functions : This function take three argumnets
Request : The inital request
Path to the Template : This is path relative to the TEMOPLATE options in the project setting.py
Dictory of parameters : a dictory that contain all varibles needed for template
Model
Model is a class that represent the table and colletion in Data Base they are define in model.py file
After creating Model type $python manage.py syncdb
we can easily manuplate the data in Models
Page Redirecting
we can re-direct the user as we want because of certin resions or errors
redirct method take as a argumnet you want to redirect as a string
from django.shortcuts import render, redirect
Sending E mails
It come along pre- install with SMTP package hostname, server name password contetct etc
from django.core.mail import send_mail
we can also attch details to the email and we can send one email to multiple peoples
Generic Views
In some case writting view as have seen earily is reallly heavy It offer a simple view that is called a generic view
It offers a set of classes not functions classes that inherit one another
import django.views.generic there are 10 views
Form Processing
It comes along with packeges and is vry easy to use them
from django import forms
we have to define all logic in the class() we need to define inherit Django classes with form attribute
File Upload
It is useful for Uploading files
Make sure you have install PIL library in Django
It take the Data and verify befor we upload on the server
Server
as far we have this Python server But it is only foe testing and development purpose in real world seniro we will use server like Apache and Ngnix
wsgi.py is the file that we link with the Django and Server
we will have to define the pathe in the file and detils
Cookies
we want to store data for the client side or visitors data as per request
Sessions
from client side we wnat to store useful data on web server
Have sessions foe Cookies and client side cookies have session ID for identifications
'django.contrib.sessions.middleware. SessionMiddleware'
information is store on Database and then
Caching
Means we want result of something as a calculation that we don,t perform
Django comes along with the catching system For used in Dynamic frameworks
Caching is done in Database and or file system and is done in Setting,py file
python manage.py createcachetable
we can catch the entire site and we can also cache a page as we want
Comments
The comments framework makes it easy to attach comments to any model in your app.
then edit the setting.py file
python manage.py syncdb
Ajax
It is used to reduce the number of pages load Mostaly used for best user experience
we need to download the dajax library with witch we can develop easily and quickly in Loic in app in Javavscript
$ pip install django_dajax
RSS
It comes along with the RSS by django.contrib.syndication.views.Feed class
we can creat a latest field of comments done on the app define logic in feed.py and we can put feed where we want
Sessions
session is a way with witch we can remember who is the user and what he have done in the app
Store session data on the server we have to import them to use
Data base Managment System
we can use differt models as we want in app Mostaly use SQL and store data
MySql and PostgrSql are more heavy types of data base SQLlite is liter form we can store data in a file
we can define the Queries in the Python classes
>SQLite Type
It support Text Numbers , Integer, Blogs bianay data audio or images , Reals Numbers
MYSQL
It support much more than sql char,varachar, smallint, int ,Bigint,Floats, Double, large Nmbers
we have to creat database as Table
Tables
we have to creat a table first and define all things in it as Class and Name
we have to define the Name and data types in the class
Auto Increment allows us to update the data as we enter the Data
Constarins : Not Null , Check,Default and more also Data also need to be valid
Insert
it is used to insert the data to the class Or Object we have defined
Data will be enter behind the scen in the Object defined
SELECT
It is used to select the Data fom The database and show in the UI or something else
data is store in form of row and coloums
SELECT * FROM flight: it will select all colums from a Object
Scalibality
There are certin limitaion of a server it can handel limited nubers of requests
So we use vaertical and Horizonatl scalling Mostaly use Horizonatl Scalling
Load Balancer is a haradware that diverge the Number of request to different server of app
Load Balancer Method
A app can have 10 or more server for the app
Random choice send request randomaly
Round Robine : First Requesrt is send to the first server and second is to the second similRY
Problums Log
If a user is login in to App aand is redirected to sercer One Next time if he is logged into another server and willnot be authenticated with session
so to identify the user we have ssveral methods
Sticky Session
It will look if a user is loogin in to 1 server will be diverted to One server and Knows him and store information on server
Session in Database
All sereves have access to data nase and Data is store in the Data base
So all data is store in Datbase and server find out if a user is login and what are other Information of a user and reconize a person
Client Side Session
When A PERSON VISIT A SITE is Issused a Cookies ansd that hold information of a user
On each request cookies are also send to the server to identify user
Auto Scalling
having many server alike if a server failed out what will happen
Load balancer will send a Ping to server if response is not getting back then it wil know a servicer is Fails and will not send request of user to that server
Database Scaling
in a app server Databases are running on Onther servers and they have acces to then
They also have limitaion so we have Partition on the databases and different parts of app can handel difernet form of Databases
If Database server fail noNe of our App is going to work so we have
Database Replication
Single-Primary Replication : Only One main function can write and update data and others can only read data If there are to many user comming and updating data It might Often Fail
Multi-Primary Replication : Threr are multiple ways we van read and write a Database But all Databases nedd to be Synchonrixe update all at same time
Mostaly Multi-Primary is useef they often have some issues It a user deleta a data and One user Updata same Data at the same time them what will happen we have to define all details
Cataching
suppose we have a front pages of a App and page get data from Database and show result on each request
If number of request Increase then alot of load comes on the pages and app It make it solw
we can define it as Storing save version of Data that we can access it more quickly so we don,t need to make Number of requet to our data base
One way is to do is On the Browser Side we can ask the client Browser to save the copy of the Page so next time user make request local it from the LOcaly not from the Database
CatchinG
Browser often do it because don,t make reqeuest and load data from the local storage
It make a process faster and good to server don,t make load on server
Cache-Control : maz-age=8600 when this time in seconds pass it make new version of page
Server Side catching
we can also persform server side Catchiog
we have multiple servers and these servers are communicating with each other and Database and Cache
There we store information that we don,t want to load rather to again recalculate
Djando also have a caching framework Pre-view catching