Table of contents
Django is a powerful web development framework used for creating creative websites. Django is well-documented and supported by a large development community. Django appears to be a MVT framework(Model-View-template).It runs on various os platforms(windows,linux,macos). Django is an open-source framework for back-end development and it supports html,css, javascript, and bootstrap.With the help of middleware, which is used to connect front end with back end,django supports full-stack development. Thus, with the help of Django, we can create a scalable full-stack web application. The database connection is also aided by the framework which makes the framework more dynamic. Sessions handling in Django helps to store caches of the web app in a very efficient way.
To master a framework,we have to know how the framework works.Lets look into the architecture of the framework.
MVT ARCHITECTURE:
Django MVT is a software design pattern which stands for Model-View-Template.
Here,look at the image.The entire working architecture of the django project is picturized in a nullshot.Views(merely a programming function) get the app data from the models(tables of the database) and process it and sends it to templates(HTML) to display for the user in the frontend.The entire backend process depends on the views.The individual process is explained further in the following passages.
Models are the tables of the database which has the entire data of your web app.Models are written in models.py file.Each model in the web represents a separate database table and each table has its own attributes.We can retrieve the data from the models through queries.The speciality of Django in retrieving data is that we don't want to write SQL queries to get the data.Instead,we can use **ORM(Object Related Mapping).**ORM is used to query the table in pythonic way.Django allows us to delete,insert,update data in the model through this API.Django ORM comes up with various in-built tools which makes the query process in a much faster pace.ORM is based on object oriented programming and it connects OOP with database and simplifies the interaction between the relational database and the web app.For example,to get all the rows in the table,instead of writing "select \ from tablename*",we can write "tablename.Objects.all()".The output type will be json as ORM is an API.To get the data of a certain person,instead of writing "select \ from table where name='name'*,we can write 'tablename.Objects.filter(name='name')".
A sample ORM call:
Here,Indianspots is a table which has the app data[Indianspots is a model].Indianspots.Objects.filter() is used to get the rows which fullfils the condition given through the filter function[here,name=place].This call is similar to the sql query='select * from Indianspots where name=place'
Views are the functions of the web app and each view is mapped to a specific url.Views will take a web request and do the specific task of the function and returns a web response.Each view is mapped to a specific url and whenever the url is hit ,the concern request will be sent to the specific view and the view will perform certain tasks and returns the response.
Here,whenever 'app/india' is hit,a function based view named india will be called with the request.
The web response will either be in the form of web content or JSON.All the backend logic implementation and web redirection or web page rendering will be done in Views.The views are written in views.py file.Business logics and data retrieval are done in Views.There are two type of views-class based views and function based views.For beginner level,you can practise with function based views.A sample view follows for a better understanding.
def lobby(request): //a function(view) named lobby is declared .It takes request as a function parameter.
return render(request,"lobby.html) // this view return a HTML page named lobby.html.This render function takes three arguments[request object,html page and a dictionary(optional),here,Dictionary is used to pass variables and arguments to the html page and the variables can be accessed via key(will be discussed in templates)]
return render (request,"lobby.html",'{''name":"vikas"}')// the name vikas is passed to the html page and can be consumed by html via jinja code.
A sample view:
This function based view simply renders (loads) the html page.
This view renders(loads) a html page
A sample view with some functionality:
Templates are the text documents of the website.Template provides a convenient way to work with dynamic html pages.All the html pages will fall under templates.Primarily,we have to create a template folder in order to use it as a container for the html pages.We have to add these templates to the base directory for easier enroutement.
In settings.py in the django app,we have to create a folder named template and join the folder with the base directory
dir=[os.path.join(BASE_DIR,'template')]
Here, all the html pages are created in template folder and are consumed by specific views to provide a smooth user interface.
NOTE:jinja is powerful template engine through which Python variables can be accessed in a html page.
{'name':'vikas}
<h1>{{name}}</h1> // displays vikas in the html page.
we can write a for loop in a html page with the help of jinja
{ % for x in range(0,5) %}<h3>{{x}}</h3>{% endfor %}
Cool feature right.These kind of stuffs makes django more versatile.
Here,we are using the for loop inside a html page to make this more dynamic and this prevents us to write repeated html tags.The python variables are passed to the html page via {{}} jinja tag.The loop is represented in jinja via {% %}.
Summary:
Django handles MVT architecture in a very efficient way which makes the developers to understand the working of the framework and the use the framework to the fullest.
Here ends the current blog.Thanks for reading.