Skillversitas

Function Views and Request/Response

The function view is the 1 line: the request the arg, the response the return. The request is the 5 the read (the GET the POST, the user, the session, the headers), and the response is the 4 the type (the HttpResponse, the JsonResponse, the redirect, the 404). The decorator is the gate (the login_required, the permission the no the 403), and the shortcuts is the 3 (the get_object_or_404, the redirect_to, the render). This chapter covers the request (the 5 the read, the immutable), the response (the 4 the type, the status), the POST (the CSRF, the form), and the decorator (the login_required, the no the 403).

The request: The 5 read

# the REQUEST (the 5, the read, the
#   no the write):
def note_list(request):
    #   the 1: the GET (the query,
    #     the immutable, the copy):
    q = request.GET.get("q", "")
    #   the no: the request.GET["q"]
    #     (the KeyError, the no the
    #     the default)
    #   the 2: the POST (the form,
    #     the CSRF's the no the 403):
    if request.method == "POST":
        title = request.POST.get(
            "title", "").strip()
    #   the 3: the user (the auth, the
    #     the no the anonymous):
    if request.user.is_authenticated:
        uid = request.user.id
    #   the 4: the session (the dict,
    #     the signed, the no the
    #     the tamper):
    view_count = request.session.get(
        "note_views", 0)
    #   the 5: the headers (the no the
    #     the all, the 1 the get):
    agent = request.META.get(
        "HTTP_USER_AGENT", "")

The request is the 5: the GET (the query, the immutable, the .get the default the no the KeyError), the POST (the form, the CSRF the token the no the 403), the user (the AnonymousUser the no the login, the is_authenticated the check), the session (the dict, the signed the cookie, the no the tamper), and the META (the header, the HTTP_ prefix, the REMOTE_ADDR the IP) - the no the write (the GET the POST the immutable, the copy the .copy(), the session the write the yes) - the response is the next: the 4 the type, the status the code, the JSON the API.

The response and the POST

# the RESPONSE (the 4, the type):
from django.http import (
    HttpResponse, JsonResponse,
    HttpResponseNotFound,
    HttpResponseRedirect)

def note_create(request):
    if request.method != "POST":
        return HttpResponseNotFound(
            "the no")
    #   the CSRF (the no the token,
    #     the 403, the the no the
    #     the the no):
    #   the: the
    #     csrf_exempt (the API's, the
    #     the no the form, the no
    #     the cookie)
    title = request.POST.get(
        "title", "").strip()
    if not title:
        return HttpResponse(
            "the no", status=400)
    Note.objects.create(
        title=title, body="")
    #   the 1: the redirect (the 302,
    #     the no the POST the again):
    return HttpResponseRedirect(
        "/notes/")
#   the 2: the JsonResponse (the
#     the API, the no the HTML):
def note_api(request):
    return JsonResponse({
        "notes": list(Note.objects.
            values("title", "created"))
    })
#   the 3: the status (the 200, the
#     400, the 404, the 500, the no
#     the 1 the no)

The response is the 4: the HttpResponse (the HTML, the status the default the 200), the JsonResponse (the API, the no the HTML, the safe the XSS the no), the HttpResponseRedirect (the 302, the POST the no the again, the 12factor), and the 404 (the no the page) - the POST is the CSRF (the token the form, the no the 403, the csrf_exempt the API the no the cookie), and the PRG is the pattern (the POST the redirect the GET, the no the again the submit) - the decorator is the gate: the @login_required (the no the login, the redirect the next), the @permission_required (the no the 403), and the @require_POST (the no the method) - the rule is the one: the request the 5 the read, the response the 4 the type, the POST the CSRF, and the decorator the gate - the next chapter's class view is the generic the 5 the no the code.

The Working Rules

  • The request is the 5, the read, the no write. The GET the query (the .get the default, the no the KeyError), the POST the form (the CSRF), the user the auth (the AnonymousUser), and the session the signed (the no the tamper) - the META the header (the HTTP_ prefix), and the no the write (the immutable, the .copy()).
  • The response is the 4, the status the code. The HttpResponse the HTML (the 200), the JsonResponse the API (the no the XSS, the safe), and the redirect the 302 (the PRG, the no the again) - the 404 the no the page, the 500 the exception (the DEBUG the traceback, the no the production), and the JsonResponse the no the encode (the 200 the no the 500).
  • The POST is the CSRF, the PRG the pattern. The token the form (the no the 403), and the csrf_exempt the API (the no the cookie, the no the token) - the PRG the 12factor (the POST the redirect the GET, the no the again), and the 400 the no the validate (the no the 500, the no the DB).
  • The decorator is the gate, the no the 403. The @login_required the login (the redirect the next, the no the 403), and the @permission_required the permission (the no the 403) - the @require_POST the method (the no the 405), and the no the view (the gate the no, the logic the no, the no the 2 the no).

Summary

The function view is the 1 line: the request the 5 the read (the GET the query, the POST the form, the user the auth, the session the signed, the META the header, the no the write), and the response the 4 the type (the HttpResponse the HTML, the JsonResponse the API, the redirect the 302, the 404 the no). The POST is the CSRF (the token the no the 403, the csrf_exempt the API), and the PRG is the pattern (the POST the redirect the GET, the no the again) - the decorator is the gate (the @login_required the redirect, the @permission_required the 403, the @require_POST the 405). The next chapter's class view is the generic the 5: the ListView the QuerySet the template, the DetailView the pk, the CreateView the form, the UpdateView the no the redirect, and the DeleteView the confirm (the no the code, the no the view).