Django FBV/CBV, middleware, GIT use

s5day82
//Content review:
    1. Http Request essence
        Django Procedure: socket Server side
            a. Server monitoring IP And port
            c. Acceptance request
                    \r\n\r\n: Request header and body
                    \r\n
                    &
                    request.POST
                    request.GET
            d. Response:
                    //Response header: location: www.oldbyedu.com
                    //and
                    //Response body
            e. Disconnect
                    
        //Browser: socket client
            b. Browser send:
                GET Request:
                    "GET /index.html http1.1\r\nUser-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x6..\r\n\r\nAccept-Encoding:gzip\r\n\r\n"
                POST Request:
                    "POST /index.html http1.1\r\nUser-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x6..\r\n\r\nAccept-Encoding:gzip\r\n\r\nuser=cqz&hobby=lihao"
        
            e. Disconnect
        
        
        COOKIE: Presence in request and response headers
    
    2. Django Requested lifecycle
        wsgi            ->    middleware     ->  Routing system   ->    View function( ORM,Template,Render)
            - wsgiref
            - uwsgi
            
        
        PS: Middleware FBV&CBV
    
    
    3. FBV and CBV
        function base view, URL Corresponding function
        class base view,    URL Corresponding class
        
        
        PS: 
            form Form submission:    GET,POST   ***
            
            Ajax Submit data:    GET,POST
                              ['get'Obtain, 'post'Establish, 'put'To update, 'patch'Partial update, 'delete'delete, 'head', 'options', 'trace'] -> restful Standard
    
        
        a. Basic use
        
        b. Be based on dispatch And inheritance to implement user login code
        
        c. Decorator
            get,post Method
                        class LoginView(View):
                            
                            def dispatch(self, request, *args, **kwargs):
                                return super(LoginView,self).dispatch(request, *args, **kwargs)

                            def get(self,request):
                                return render(request,'login.html')

                            @method_decorator(test)
                            def post(self,request):
                                # request.GET
                                # request.POST # Content type in request header
                                # request.body
                                user = request.POST.get('user')
                                pwd = request.POST.get('pwd')
                                if user == 'alex' and pwd == "alex3714":
                                    # Generate random string
                                    # Write browser cookie:  session_id: Random string
                                    # Write to server session: 
                                    # {
                                    #     "Random string": {'user_info':'alex}
                                    # }
                                    request.session['user_info'] = "alex"
                                    return redirect('/index.html')
                                return render(request, 'login.html')
            dispatch Method
                class LoginView(View):
                    @method_decorator(test)
                    def dispatch(self, request, *args, **kwargs):
                        return super(LoginView,self).dispatch(request, *args, **kwargs)

                    def get(self,request):
                        return render(request,'login.html')

                    
                    def post(self,request):
                        # request.GET
                        # request.POST # Content type in request header
                        # request.body
                        user = request.POST.get('user')
                        pwd = request.POST.get('pwd')
                        if user == 'alex' and pwd == "alex3714":
                            # Generate random string
                            # Write browser cookie:  session_id: Random string
                            # Write to server session: 
                            # {
                            #     "Random string": {'user_info':'alex}
                            # }
                            request.session['user_info'] = "alex"
                            return redirect('/index.html')
                        return render(request, 'login.html')
            //Class
                @method_decorator(test,name='get')
                class LoginView(View):
                    
                    def dispatch(self, request, *args, **kwargs):
                        return super(LoginView,self).dispatch(request, *args, **kwargs)

                    def get(self,request):
                        return render(request,'login.html')


                    def post(self,request):
                        # request.GET
                        # request.POST # Content type in request header
                        # request.body
                        user = request.POST.get('user')
                        pwd = request.POST.get('pwd')
                        if user == 'alex' and pwd == "alex3714":
                            # Generate random string
                            # Write browser cookie:  session_id: Random string
                            # Write to server session: 
                            # {
                            #     "Random string": {'user_info':'alex}
                            # }
                            request.session['user_info'] = "alex"
                            return redirect('/index.html')
                        return render(request, 'login.html')
                        
            //Special: CSRF Token can only be added to dispatch
            
                
                    from django.views.decorators.csrf import csrf_exempt,csrf_protect
                    class LoginView(View):
                        @method_decorator(csrf_exempt)
                        def dispatch(self, request, *args, **kwargs):
                            return super(LoginView,self).dispatch(request, *args, **kwargs)

                        def get(self,request):
                            return render(request,'login.html')


                        def post(self,request):
                            # request.GET
                            # request.POST # Content type in request header
                            # request.body
                            user = request.POST.get('user')
                            pwd = request.POST.get('pwd')
                            if user == 'alex' and pwd == "alex3714":
                                # Generate random string
                                # Write browser cookie:  session_id: Random string
                                # Write to server session: 
                                # {
                                #     "Random string": {'user_info':'alex}
                                # }
                                request.session['user_info'] = "alex"
                                return redirect('/index.html')
                            return render(request, 'login.html')

    4. middleware
        a. Middleware is a class
            - process_request
                - Yes, directly execute the current middleware and the upper middleware process_response
                - nothing
                //Application: user login authorization (excluding URLs that do not require login)
            - process_response
                - Must have return value
            
            - process_view
            
            - process_exception
            
            - process_tempalte_response
                - Must have return value
                - Must have render Method
            
        b. Technological process
        
        c. When to use middleware? Use when all requests are processed uniformly
            - validate logon 
            
            
        d. There can be any method in middleware

Example:
settings.py
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',
    'md.middleware.M1',
    'md.middleware.M2',
]

Project/md/middleware.py
from django.shortcuts import HttpResponse,redirect
class MiddlewareMixin(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        super(MiddlewareMixin, self).__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        if not response:
            response = self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response

"""
class M1(MiddlewareMixin):

    def process_request(self,request):
        print('m1.process_request')

    def process_view(self,request, view_func, view_func_args, view_func_kwargs):
        print('m1.process_view')

    def process_exception(self,request,exception):
        print('m1.process_exception')

    def process_response(self,request,response):
        print('m1.process_response')
        return response

    def process_template_response(self,request,response):
        print('m1.process_template_response')
        return response

class M2(MiddlewareMixin):
    def process_request(self, request):
        print('m2.process_request')

    def process_view(self, request, view_func, view_func_args, view_func_kwargs):
        print('M2.process_view')

    def process_exception(self,request,exception):
        print('m2.process_exception')
        return HttpResponse('Developers have been killed')
    def process_response(self, request, response):
        print('m2.process_response')
        return response

    def process_template_response(self,request,response):
        print('m2.process_template_response')
        return response
"""

class M1(MiddlewareMixin):

    def process_response(self, request, response):
        print('m2.process_response')
        return response


class M2(MiddlewareMixin):
    def process_request(self, request):
        print('m2.process_request')

 

        
Summary of today's content:
    1. Django content supplement
        - FBV&CBV
        - Middleware
    2. Git use
        -What is GIT,
        -Open story: resource sharing website
            -Little brother series
            -Xiaofang series
            
        -First version: enter the program directory:
            git init initialization
            git add. Add all files in the current directory to [somewhere]
            git commit -m 'description information' the first version of git has been generated
            
            git status
            
        -New function: Li Hao area
            
            Halfway to development, emergency repair online BUG
            
            git stash
            
            Solve bug
            git add .
            git commit -m 'bug fix completed'
            
            
            git stash pop 
                There may be conflicts. Don't worry about solving them manually
                Auto-merging templates/index.html
                CONFLICT (content): Merge conflict in templates/index.html
                
            Development function completed
            
            git add . 
            git commit -m 'after resolving the conflict, continue to develop subsequent functions'
                
            
            PS: 
                git stash stores all the modified content in the current workspace to "somewhere", and restores the workspace to the status that has not been modified in the current version
                git stash pop takes the first record from "somewhere" back to the workspace (possible conflict)
                Git stash listview all records stored in "somewhere"
                git stash clear clear "somewhere"
                git stash apply number to retrieve the specified number record from "somewhere" to the workspace (possible conflict) 
                git stash drop number, delete the record with the specified number
            
            
        Questions:
            git log 
            git reset --hard  3ee80517425148b9d87591c9bd29a77e3db89ff2
            git reflog

            
            
            
        
    
    
    
    
    
    
    
    
    
    
    
    

Tags: Python git Django Session socket

Posted on Tue, 31 Mar 2020 01:58:35 -0700 by ruano84