Django request by the knowledge life cycle extending large sum

django project to build See:

https://www.cnblogs.com/dongxixi/p/10981577.html

FIG django request lifecycle :

 

 

Initiated by the browser request to start

Knowledge Point 1:

Browser and server software architecture: C / S architecture

HTTP protocol: also known as the Hypertext Transfer Protocol, which defines the format of the browser and the server data transmission

HTTP protocol four characteristics:

    TCP / IP based application layer protocol acting

    Based on a request response

    no status

    no connection

Knowledge Point 2:

Request format and response format:

  Request format:

    The first line request

    Request header

    Blank line (\ r \ n)

    Request body

  Response format:

    The first line of response

    Response header

    Blank line

    Response Body

 

 

request response processing wsgiref

Knowledge Point 3: web service gateway protocol

cgi: Common Gateway Protocol

wsgi agreement:

  wsgi protocol (Web Server Gateway Interface) and application server consists mainly of two parts:

  • WSGI serverIs responsible for receiving a request from a client, will be requestforwarded to applicationthe applicationreturn responseback to the client;
  • WSGI application is received by the server forwards the request, processes the request, and returns the processing result to the server. application may include a plurality of stacks of intermediate of formula (middlewares), which middleware and application server need to implement, and therefore can play a regulatory role in between WSGI WSGI application and the server: the server, the application middleware play ( execution of the program), for applications, middleware plays servers (WSGI server).

WSGIActually defines a protocol serverand application解耦specification, we django comes wsgiref is the realization of the agreement

In addition there are many other servers implement the agreement:

uwsgi:支持较高并发,django项目上线一般会选择用它替换django自带的wsgiref    == JAVA中的tomcat

wsgiref:支持并发不高,django自带

 

请求解析完成后依次通过Django中间件

知识点4:django中间件

django自带七大中间件:可以简单理解为django的门户,安全认证及全局处理都在这里面

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

除了django自带的中间件以外我们还可以自定义中间件实现一些全局的校验和配置

方法:在应用下新建文件夹,名字自取,文件夹下新建任意py文件,名字自取,接下来就可以在py中定义自己的中间件l

# 导入MiddlewareMixin模块
from django.utils.deprecation import MiddlewareMixin

#定义中间件的类,它继承MiddlewareMixin
class Md1(MiddlewareMixin):
    def process_request(self, request):
        print('Md1里面的process_request')


class Md2(MiddlewareMixin):
    def process_request(self, request):
        print('Md2里面的process_request')

django暴露给开发者5种方法分别是:

process_request(self, request):   #请求来的时候执行
process_response(self, request, response):  #响应返回时执行
process_template_response(self, request, response): #视图函数执行完了执行
process_view(self, request, view_func, view_args, view_kwargs): #路由匹配成功后视图函数执行前执行
process_exception(self, request, exception): #视图函数出错时执行

中间件具体作用及执行流程详见:https://www.cnblogs.com/dongxixi/p/11048901.html

 

接下来进入urls.py,路由匹配

知识点5:路由系统

    urlpatterns = [
                url(r'^admin/', admin.site.urls),
                url(r'^$',views.home),
                url(r'^test/$',views.test),
                url(r'^testadd/$',views.testadd),
                url(r'',views.error)
            ]

①路由正则匹配

②无名分组

③有名分组

④反向解析

⑤路由分发

路由系统详见:https://www.cnblogs.com/dongxixi/p/11001722.html

 

路由匹配成功,进入视图函数

知识点6:CBV/FBV

FBV:

  路由层写法: 

   url(r'^test/$',views.test),

  视图层写法:

def text(request):
    if request.method == "POST":
        return HttpResponse("ok")
    elif request.method == "GET":
        return HttpResponse("ok")

 

CBV:

  路由层写法:

 url(r'^test/$',views.Text.as_view()),

  视图层写法:

from django.views import View
class Text(View):
    def get(self,request):
        return HttpResponse("ok")
    def post(self,request):
        return HttpResponse("ok")

三板斧及jsonresponse

from django.shortcuts import render,redirect,HttpResponse
from django.http import JsonResponse

 

CBV源码剖析见:

 

视图函数处理数据,进行django模板渲染

知识点7:django模板层

①过滤器

②标签

③自定义过滤器、标签、inclusion_tag

④模板继承与导入

⑤静态文件配置

详见:

模板层:https://www.cnblogs.com/dongxixi/p/11013803.html

补充:https://www.cnblogs.com/dongxixi/p/11007339.html

 

知识点8:cookie与session

①cookie基本使用

②session原理及使用

③auth模块

④自定义User表

 

cookie与session使用见:https://www.cnblogs.com/dongxixi/p/11048974.html

auth模块及自定义User表使用见:https://www.cnblogs.com/dongxixi/p/11055516.html

 

知识点9:form认证组件

①form组件使用

②form组件进阶

③分页器

form组件:https://www.cnblogs.com/dongxixi/p/11042917.html

form组件进阶:https://www.cnblogs.com/dongxixi/p/11043435.html

分页器:https://www.cnblogs.com/dongxixi/p/11042854.html

 

知识点10:django数据库操作:

①一对一、一对多、多对多表分析及创建

②多对多表三种创建方式

③django ORM增删改查、单表操作、多表操作

④F、Q查询

⑤ORM类、字段基础

⑥事务、批量插入、查询优化

表关系:https://www.cnblogs.com/dongxixi/p/10862974.html

多对多表三种创建方式:https://www.cnblogs.com/dongxixi/p/11042883.html

ORM增删改查:https://www.cnblogs.com/dongxixi/p/11013783.html

F、Q查询:https://www.cnblogs.com/dongxixi/p/11042869.html

ORM基础:https://www.cnblogs.com/dongxixi/p/11050753.html

django-model进阶:https://www.cnblogs.com/liuqingzheng/articles/9805991.html

 

知识点11:

未完待续...

 

Guess you like

Origin www.cnblogs.com/dongxixi/p/11115921.html