zoukankan      html  css  js  c++  java
  • 视图

    视图

    请求相关的属性方法(request--HttpRequest对象)

    def index(request): #http相关请求信息---封装--HttpRequest对象
    
        if request.method == 'GET':
            print(request.body)  #获取post请求提交过来的原始数据
            print(request.GET)   #获取GET请求提交的数据
            # print(request.META)  # 请求头相关信息,就是一个大字典
            print(request.path) #/index/ 路径
            print(request.path_info) #/index/ 路径
            print(request.get_full_path())  #/index/?username=dazhuang&password=123
            
            return render(request,'index.html')
        else:
            print(request.body)  # b'username=dazhuang'
            print(request.POST) #获取POST请求提交的数据
            return HttpResponse('男宾三位,拿好手牌!')
    
    

    响应相关的方法

    HttpResponse  --- 回复字符串的时候来使用
    render --- 回复一个html页面的时候使用
    redirect -- 重定向
    	示例:
    	def login(request):
            if request.method == 'GET':
                return render(request,'login.html')
            else:
                username = request.POST.get('username')
                password = request.POST.get('password')
                if username == 'taibai' and password == 'dsb':
                    # return render(request,'home.html')
                    return  redirect('/home/')  #重定向
                else:
                    return HttpResponse('滚犊子,赶紧去充钱!!!')
    
        #首页
        def home(request):
            return render(request,'home.html')
    
    

    FBV和CBV

    FBV -- function based view
    def home(request):
        print('home!!!')
        return render(request,'home.html')
    
    CBV  -- class based view
    
    views.py
        from django.views import View
        class LoginView(View):
            # 通过请求方法找到自己写的视图类里面对应的方法
            def get(self,request):
    
                return render(request,'login2.html')
            def post(self,request):
                username = request.POST.get('uname')
                password = request.POST.get('pwd')
                print(username,password)
    
                return HttpResponse('登录成功!')
                
    urls.py
    	url(r'^login2/', views.LoginView.as_view()),
    

    CBV通过不同的请求方法找到对应的试图类中的方法

    关键点,反射

        def dispatch(self, request, *args, **kwargs):
            if request.method.lower() in self.http_method_names:
                handler = getattr(self, request.method.lower(), self.http_method_not_allowed)  #反射
    

    CBV的dispatch方法

    from django.views import View
    class LoginView(View):
        # GET 
        def dispatch(self, request, *args, **kwargs):
            print('请求来啦')
            ret = super().dispatch(request, *args, **kwargs)
            print('到点了,走人了')
            return ret
        def get(self,request):
            print('get方法执行了')
            return render(request,'login2.html')
        def post(self,request):
            username = request.POST.get('uname')
            password = request.POST.get('pwd')
            print(username,password)
            return HttpResponse('登录成功!')
    

    FBV加装饰器

    def n1(f):
        def n2(*args,**kwargs):
            print('请求之前')
            ret = f(*args,**kwargs)
            print('请求之后')
            return ret
        return n2
    @n1
    def home(request):
        print('home!!!')
        return render(request,'home.html')
    

    CBV加装饰器

    from django.views import View
    from django.utils.decorators import method_decorator
    
    def n1(f):
        def n2(*args,**kwargs):
            print('请求之前')
            ret = f(*args,**kwargs)
            print('请求之后')
            return ret
        return n2
    
    # @method_decorator(n1,name='get') #方式三
    class LoginView(View):
        # GET
        # @method_decorator(n1)  #方式2 给所有方法加装饰器
        def dispatch(self, request, *args, **kwargs):
            # print('请求来啦')
            ret = super().dispatch(request, *args, **kwargs)
            # print('到点了,走人了')
            return ret
    
        # @method_decorator(n1)  #方式1
        def get(self,request):
            print('get方法执行了')
            return render(request,'login2.html')
        def post(self,request):
            username = request.POST.get('uname')
            password = request.POST.get('pwd')
            print(username,password)
            return HttpResponse('登录成功!')
    
  • 相关阅读:
    为什么折价溢价的摊销属于借款费用?
    长期待摊费用
    合同负债
    未决诉讼
    AMD Ryzen 3600超频4.3
    强制删除软件
    wallpaper壁纸download下载失败
    ryzen3600超频4.3
    维度规约(降维)算法在WEKA代写中应用
    python代写虎扑社区论坛数据爬虫分析报告
  • 原文地址:https://www.cnblogs.com/wyh0717/p/13545108.html
Copyright © 2011-2022 走看看