In this blog, Django will be getting chained by the Django Master himself, just like the old times.
Django is a Batteries-included WEB Framework written in Python. Django takes care of the difficult stuff so that you can concentrate on building your web application. But as a Django Master, I don't need Django to take care of the difficult Stuff for me. Django emphasizes reusability of components, also referred to as DRY (Don't Repeat Yourself) and comes with ready-to-use features like login system, database connection and CRUD operations (Create Read Update Delete).
Flow of Data in a Django Server
Request-Response Flow
When a Request is received by the Django server, Some Magic happens, and a Response is Generated. I've devised a Very Detailed Flowchart of the process below:
How does the "Algorithm" work?
As a Django Master, I like to explain the Algorithm as:
When A Request is received by the Server. The Server Looks at the URL that the request is looking for. Then It will look for the Function assigned to handle that URL request. It invokes the Function that will return a Webpage, that gets sent as the Response To that request.
What if the URL doesn't have a function?
Then Obviously an Error Occurs. If you needed to "Know" what happens if URL doesn't have a Function that can handle it. then I suggest you stop reading all blogs from the Django Master. Your monkey brain can't handle the amount of knowledge in upcoming Blogs.
What is the Architecture that Django Uses?
Django Uses MVT architecture.
MVT stands for Model-View-Template
If someone says Django is MVC architecture. Your Response to that request should be, "YOU'RE WRONG AND I HATE YOU!!!"
Below is a Detailed Figure that demonstrates MVT:
If you're not a Django master, you probably Need more explanation on the Figure.
Model: Model is the Database for Django. It is written in Models.py
View: View are the function that will take the data out of database. It is written in Views.py
Template: Template is a Skeleton of the HTML file that wont have any data. It is written in Templates folder
Here is Examples of Model, View and Template's Code:
Models.py:
from django.db import models
class Message(models.Model):
messageid = models.AutoField(primary_Key = True)
content = models.CharField()
views.py:
from django.shortcuts import render
from .models import Message
def index(request,message_id):
template_path = 'templates/index.html'
msg = Message.objects.get(messageid=message_id)
context = {
'message':msg,
}
return render(request,template_path,context)
Templates/index.html
<!Doctype HTML>
<html>
<head>
<title> Messages </title>
</head>
<body>
<p> The Message is: {{messgage.content}} </p>
</body>
</html>
Thats the Basics of Django. In next blog, We'll start Creating Best TODO Apps the world has ever seen with the Django Master Nischal lamichhane .