Model View Controller
- MVC stands for Model View and controller
- Model: This porvides the interface for the data stored in databases
- Views: This is an user interace. It is responsible for displaying Model data to the user and also take up information from users
- Controller: A controller in MVC is responsible for the entire logic behind the web application.
Django MVT Architecture
-
MVT Stands for Model view Template
- Model: Just like the Model in the MVC, here we have some functionality of providing the interface for the data stored in database
- Template: This is like View in MVC. Django uses templates in its framework. Templates are responsible for User Interface.
- Views:
- In Django Views act a link b/w Model data and templates
- View will see the user request retrieves appropriate data from databases, then renders back the template along with retrieved data
-
Control flow of MVT
- User send a URL Request for a resource to Django
- Django framework search for the URL resource
- If the URL path links up to view, then particular view is called
- View will interact with model and retrieve the appropriate data from database
- The View then renders back an appropriate template along with the retrieved data to the user
-
Lets Dive little bit more into this
Models
- Django models define the data for your application and provide an abstraction layer to SQL database access through Object Relational Mapper (ORM).
- ORM lets you define the data schema using python code, without needing to understand of the underlying database.
- This means we can define database layer in python code and Django will take care of generating SQL Queries for us.
- We can choose many SQL database servers such as SQLite, PostgreSQL, MySQL or Microsoft SQL Server
- Django’s ORM takes care of these differences for you.
Views
- A Django View is where most of the logic for our applciation is defined.
- A view is a function that we write which will receive the request (made by user from webbrowser) in the form of Python Object (Specifically, a Django HttpRequest Object).
- It is up to view to decide how it should respond to the request and what it should send back to the user.
- View should return an HttpResponse object
Templates
- A template is a HTML file that contains special placeholders that can be replaced with varibales your application provides.
- For example, if our application could render list of items , view would fetch the same models and render a HTML file with information fetched from models
Introduction to HTTP
-
Web browser create an HTTP request, which is sent to the server hosting website
-
Once the webserver recieves the HTTP request, it can interpret it and then sends back a response.
-
The response that the server sends might be simple such as an HTML file or image etc.
-
Request is made up of four main parts
- Methods: HTTP protocol has methods, some of the common ones are
- GET (retrieve the remote page/data)
- POST (send data to the remote)
- PUT (Create request with data)
- DELETE (delte some resource)
- path
- headers: contain the data sent from browser to webserver, lets look at some of the most commn headers
- Host
- User-Agent
- Cookie
- Content-Type
- Authentication Headers
- body
- Methods: HTTP protocol has methods, some of the common ones are