Django框架之视图层(views)介绍

一、视图层类型

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')

2、需要思考的问题

2.1 为什么能够根据前端请求类型执行对应方法?

2.2 路由层中 views.MyCls.as_view()是什么?

  MyCls是类,as_view是方法或者属性;通过MyCls.as_view()可以确定as_view应该是方法。与此同时MyCls类调用它,无参数传入,所以它并且是类方法。
当类点属性或方法时,它的查找顺序时:自身-->父类。在MyCls类中无as.view方法,因此它是父类View中的方法。
  我们在浏览器输入地址能够与 ^mycls/ 匹配成功时,执行MyCls.as_view(实际是拿到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)
MyCls继承的View类的部分源码,重点在注释部分

二、关于JsonResponse

  在开发过程中前端,后端使用的语言类型是不同的,那我们是怎么进行前后端数据交互的呢?或者说是否有一种通用的格式,能够都被前后端识别并做相互的转化?这里就需要使用Json数据交换的文本格式。

#返回给前端jason格式的字符串

#路由层
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')

猜你喜欢

转载自www.cnblogs.com/yuanlianghong/p/11001164.html