The MVT Architecture
The MVT is the request the flow: the URL the match (the urls.py the path the view), the View the logic (the request the read, the QuerySet the fetch), the Model the data (the QuerySet the SQL the DB), and the Template the HTML (the render the variable the loop). The middleware is the before plus the after (the session, the CSRF, the auth), and the 12factor is the separation (the code the config, the DB the build). This chapter covers the flow (the 6 step), the Model (the QuerySet, the no the SQL), the View (the request, the response), the Template (the render, the escape), and the middleware (the hook, the order).
The flow: The 6 steps
# the FLOW (the 6 step, the request to the
# response):
# the 1: the WSGI (the gunicorn the
# the request the receive)
# the 2: the middleware the BEFORE
# (the session the load, the CSRF the
# the check, the auth the user)
# the 3: the URL the resolve (the
# the urls.py the path the match,
# the kwargs the capture)
# the 4: the View the call (the
# the request the read, the QuerySet
# the fetch, the response the build)
# the 5: the middleware the AFTER
# (the response the modify, the
# the session the save)
# the 6: the WSGI the send (the HTTP
# the bytes the client)
# the the 3: the 4xx/5xx the
# the no match (the 404), the
# the View's exception (the 500)
The flow is the 6 step: the WSGI receives the HTTP, the middleware runs before (the session load, the CSRF check, the auth user), the URL resolver matches the path (the path("notes/", note_list), the int the pk the capture), the View runs (the request.GET the read, the QuerySet the fetch, the render the build), the middleware runs after (the session save, the compression), and the WSGI sends the bytes. The 404 is the no-match (the path the no the view), the 500 is the exception (the traceback the DEBUG the show, the page the production), and the 12factor is the separation (the code the git, the config the env, the DB the attached the no the build) - the Model is the data layer: the QuerySet the SQL the no the write.
The Model: The QuerySet
# the MODEL (the data, the SQL's no the
# write):
from django.db import models
class Note(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
created = models.DateTimeField(
auto_now_add=True)
def __str__(self):
return self.title
# the QUERYSET (the SQL's no the write):
Note.objects.all() # the SELECT *
Note.objects.filter(
title="x") # the WHERE
Note.objects.get(pk=1) # the 1 (the
# DoesNotExist)
Note.objects.order_by(
"-created") # the ORDER BY
Note.objects.count() # the COUNT
note.save() # the INSERT
note.delete() # the DELETE
# the the lazy: the QuerySet the no
# the SQL until the evaluate (the
# the list, the len, the 1)
The Model is the data: the field is the column (the CharField the varchar, the TextField the text, the DateTimeField the timestamp), the QuerySet is the SQL the no the write (the all the SELECT, the filter the WHERE, the get the 1, the order_by the ORDER BY), and the save/delete is the INSERT/DELETE. The lazy is the key: the QuerySet is the no the SQL until the evaluate (the list, the len, the for, the 1 the access) - the Note.objects.filter(title="x") is the 0 the SQL (the lazy), the len(...) is the 1 the SQL, and the chained is the 1 the SQL (the filter the order_by the 1 the WHERE the ORDER BY) - the N+1 is the for the related (the select_related the prefetch the fix, the queries chapter) - the View is the logic: the request the read, the response the return.
The View and the Template
# the VIEW (the logic, the request to the
# response):
from django.shortcuts import render
from notes_app.models import Note
def note_list(request):
# the GET (the query, the no the
# parse):
q = request.GET.get("q", "")
# the POST (the form, the CSRF's):
# request.POST the no the
# the token's the no (the 403)
notes = Note.objects.all()
if q:
notes = notes.filter(title__icontains=q)
return render(request, "notes/list.html",
{"notes": notes, "q": q})
# the TEMPLATE (the HTML, the escape,
# the no the logic):
# {% extends "base.html" %}
# {% block content %}
# {% for note in notes %}
# <article>
# <h3>{{ note.title }}</h3>
# </article>
# {% endfor %}
# {% endblock %}
# the {{ }} the escape (the XSS's
# the no, the |safe the no the
# the trust)
The View is the logic: the request.GET is the query (the q the filter), the request.POST is the form (the CSRF the token the no the 403), the QuerySet is the fetch, and the render is the build (the template the context the response). The Template is the HTML: the {% extends %} is the block (the base, the no the repeat), the {% for %} is the loop, and the {{ }} is the escape (the XSS the no, the |safe is the trust the no the use) - the template is the no the logic (the if the loop only, the math the View). The middleware is the hook: the before (the session load, the CSRF check, the auth) plus the after (the session save, the compression), the order is the MIDDLEWARE list (the 1 the first, the response the reverse), and the custom is the class (the __call__ the next) - the rule is the one: the Model the data, the View the logic, the Template the HTML, the middleware the hook, and the 12factor the separation - the next chapter's first-app is the startapp the Note the 1 model the 2 field.
The Working Rules
- The flow is the 6, the middleware the before and after. The WSGI the receive, the middleware the session the CSRF the auth, the URL the resolve (the path, the kwargs), the View the call, the middleware the save, and the WSGI the send - the 404 the no-match, the 500 the exception, the 12factor the separation.
- The QuerySet is the SQL, the lazy the key. The all the SELECT, the filter the WHERE, the get the 1 (the DoesNotExist), and the save/delete the INSERT/DELETE - the 0 SQL until the evaluate (the list, the len, the 1), the chained the 1 the SQL, the N+1 the for the related (the select_related the fix).
- The View is the logic, the GET the query the POST the form. The request.GET the q, the request.POST the CSRF (the token the no the 403), the QuerySet the fetch, and the render the build (the template, the context) - the request the no the mutate (the immutable, the copy the .GET the .POST).
- The Template is the HTML, the escape the XSS. The {% extends %} the block, the {% for %} the loop, and the {{ }} the escape (the |safe the no the trust) - the no the logic (the if the loop only, the math the View), and the middleware the hook (the before, the after, the order the MIDDLEWARE).
Summary
The MVT is the request the flow: the WSGI receives, the middleware runs before (the session, the CSRF, the auth), the URL resolver matches (the path, the kwargs), the View runs (the GET the read, the QuerySet the fetch, the render the build), the middleware runs after (the session save, the compression), and the WSGI sends. The Model is the data (the field the column, the QuerySet the SQL the no the write, the lazy the no the SQL until the evaluate), the View is the logic (the request the immutable, the POST the CSRF), the Template is the HTML (the extends the block, the for the loop, the {{ }} the escape the XSS), and the middleware is the hook (the before, the after, the order the MIDDLEWARE list). The 12factor is the separation (the code the git, the config the env, the DB the attached) - the next chapter's first-app is the startapp the Note the 1 model the 2 field, the admin the register, and the 2 view the list the detail the 1 template.