zoukankan      html  css  js  c++  java
  • Django之视图层

    一、视图层

    1、FBV与CBV

    FBV:基于函数的视图
    
    CBV:基于类的视图
    
    
    #使用CBV时,路由层写法
    urlpatterns = [
        #分析源码这里也可写成 url(r'^mycls/', views.view),这种格式与FBV路由层中一致
        url(r'^mycls/', views.MyCls.as_view()),] 
    '''
    FBV与CBV中路由层都是:路由对应视图函数内存地址
    '''
    
    # 使用CBV时,首先在views文件中导入View模块
    from django.views import View
    
    #定义类,继承View
    class MyCls(View):
        def get(self,request):
            return render(request,'index.html')
    
        def post(self,request):
            return HttpResponse('post')


    View中部分源码:
    #View中部分源码
        @classonlymethod        #绑定给类的方法
        def as_view(cls, **initkwargs):    #cls是我们写的MyCls
    
            for key in initkwargs:
                if key in cls.http_method_names:
                    raise TypeError("You tried to pass in the %s method name as a "
                                    "keyword argument to %s(). Don't do that."
                                    % (key, cls.__name__))
                if not hasattr(cls, key):
                    raise TypeError("%s() received an invalid keyword %r. as_view "
                                    "only accepts arguments that are already "
                                    "attributes of the class." % (cls.__name__, key))
    
            def view(request, *args, **kwargs):  #定义在as_view内部,是闭包函数
                
                #cls表示引用了as_viewde1作用域,也就是我们写的MyCls类
                self = cls(**initkwargs)    #self相当于MyCls实例化的对象
                if hasattr(self, 'get') and not hasattr(self, 'head'):
                    self.head = self.get
                self.request = request
                self.args = args
                self.kwargs = kwargs
                #self是MyCls的对象,self中没有dispatch方法,MyCls中也没有,还要从View中找
                return self.dispatch(request, *args, **kwargs)
            view.view_class = cls
            view.view_initkwargs = initkwargs
    
            update_wrapper(view, cls, updated=())
            
            update_wrapper(view, cls.dispatch, assigned=())
            return view           #调用路由层中调用as_view,会拿到 view 的内存地址
        
        
        def dispatch(self, request, *args, **kwargs): 
            # request.method获取请求类型,判断是否在self.http_method_names中
            if request.method.lower() in self.http_method_names:
            
            # 反射,self是MyCls的对象;获取MyCls中 get/post 方法,
                handler = getattr(self, request.method.lower(),
                                  self.http_method_not_allowed)
            else:
                handler = self.http_method_not_allowed
            return handler(request, *args, **kwargs)


    2、JsonResponse

    # 在开发过程中前、后端使用的语言类型是不同的,这里就需要使用Json数据交换的文本格式。
    
    
    #路由层
    urlpatterns = [url(r'^index/', views.index)]
    #视图层
    from django.shortcuts import HttpResponse
    import json
    
    #手动序列化
    def index(request):
        res = {'name':'Bob','password':123}
        return HttpResponse(json.dumps(res)) #前端结果{"name":"Bob","password",123}
    
         res = {'name':'Bob大帅比','password':123}
        return HttpResponse(json.dumps(res))#前端显示结果不能识别中文
    
        #将dumps中ensure_ascli参数改为Flase,序列化时保证数据内容不变
        return HttpResponse(json.dumps(res),ensure_ascii=False)
    
    # django自行序列化
    from django.http import JsonResponse
    def index(request):
        res={'name':'Bob大帅比','password':123}
        return JsonResponse(res,json_dumps_params={'ensure_ascii':False})


    二、简单的文件上传

    前端需要注意的点:   
      1.method需要指定成post
            2.enctype需要改为formdata格式
    
    <form action="{% url app01_index %}" method="post" enctype="multipart/form-data">
        <input type="file" name="my_file">
        <input type="submit">
    
    ########
    后端暂时需要注意的是:
       1.配置文件中注释掉csrfmiddleware中间件
       2.通过request.FILES获取用户上传的post文件数据
    
    def upload_file(request):
        if request.method == 'POST':
            print('path:',request.path)
            print('full_path:',request.get_full_path())
            # print(request.FILES)
            file_obj = request.FILES.get('my_file')
            print(file_obj.name)
            with open(file_obj.name,'wb') as f:
                for line in file_obj.chunks():
                    f.write(line)
            return HttpResponse('OK')
        return render(request,'index.html')
  • 相关阅读:
    mongodb备份与恢复
    MongoDB-3.4安装文档
    (转)Zabbix 3.2.7编译安装记录
    (转)error while loading shared libraries:libmysqlclient.so.18 错误
    (转)如何使用Journalctl查看并操作Systemd日志
    (转)基于CentOS 7安装Zabbix 3.4和Zabbix4.0
    (转)yum安装MariaDB(使用国内镜像快速安装,三分钟安装完毕)
    (转)nmon和nmon analyser的下载和使用
    (转)Db2 数据库常见堵塞问题分析和处理
    (转)我是一个线程
  • 原文地址:https://www.cnblogs.com/weiyiming007/p/11811441.html
Copyright © 2011-2022 走看看