数据分析系统,哪里有整站优化,正规网站制作公司哪里有,响应式网站尺寸节点目录
一、三板斧的使用
二、JsonReponse序列化类的使用
三、 form表单上传文件
数据准备
数据处理
(1)post请求数据
(2)文件数据获取
四、 FBV与CBV
五、CBV的源码分析
as_view 方法 一、三板斧的使用
HttpResponse 返回字符串类型render 渲染html页面#xff0c;并…目录
一、三板斧的使用
二、JsonReponse序列化类的使用
三、 form表单上传文件
数据准备
数据处理
(1)post请求数据
(2)文件数据获取
四、 FBV与CBV
五、CBV的源码分析
as_view 方法 一、三板斧的使用
HttpResponse 返回字符串类型render 渲染html页面并且在返回给浏览器之前还可以给html页面传值redirect 重定向页面 在视图文件中写视图函数的时候不能没有返回值了默认返回的是None页面上就会报错 def render(request, template_name, contextNone, content_typeNone, statusNone, usingNone):content loader.render_to_string(template_name, context, request, usingusing)return HttpResponse(content, content_type, status)
二、JsonReponse序列化类的使用
json格式的数据{a:1} json有什么用跨语言传输
序列化json.dumps 反序列化json.loads
from django.http import JsonResponse
def index(request):# user_dict {username:kevin你好}user_dict [1, 2, 3, 4]# res json.dumps(user_dict,ensure_asciiFalse)# return HttpResponse(res)# return JsonResponse(user_dict,json_dumps_params{ensure_ascii:False})return JsonResponse(user_dict,safeFalse)# return render(request,index.html)
三、 form表单上传文件
form action methodpost enctypemultipart/form-data/form form表单上传数据需要满足的条件 请求方式必须是postenctype参数必须指定成 form-data 类型 数据准备
路由
# form 表单上传 下载文件
url(^ab_file/,views.ab_file),
前端
form action methodpost enctypemultipart/form-data classform form-controlpusername:input typetext nameusername classform-control/ppfile:input typefile namefile classform-control/pinput typesubmit
/form
数据处理
(1)post请求数据
def ab_file(request):if request.method POST:# 只能获取到普通的文本数据无法获取到文件数据print(request.POST)return render(request, file.html)
QueryDict: {username: [dream]}
通过这种方式我们只能获取到我们输入的文本数据而拿不到我们想要的文件数据
(2)文件数据获取
def ab_file(request):if request.method POST:# 获取文件数据print(request.FILES) # MultiValueDict: {file: [InMemoryUploadedFile: img.png (image/png)]}# 提取文件数据 - 文件对象file_obj request.FILES.get(file)# 提取文件名字 file_obj.namewith open(file_obj.name, wb) as f:# 逐行读取文件数据# 官方推荐 加上 chunks 方法 等价于 一行行获取for line in file_obj.chunks():f.write(line)return render(request, file.html)
MultiValueDict: {file: [InMemoryUploadedFile: img.png (image/png)]}
四、 FBV与CBV
FBVfunction based view -----》写的都是函数
CBVclass based view -----》写的都是类
CBV视图
from django.views import Viewclass MyLogin(View):def get(self, request):print(get...)return HttpResponse(get)def post(self, request):return HttpResponse(hello postman!!!)url(r^login/, views.MyLogin.as_view()), MBV和CBV各有各的特点都有应用 CBV特点 能够直接根据请求方式的不同直接匹配到对应的方法执行 五、CBV的源码分析
as_view 方法
路由对应函数内存地址
url(r^login/, views.MyLogin.as_view()), 方法/函数名 加 括号 执行优先级最高 View类中得as_view方法的返回值是view函数名当请求来的时候会触发view函数的执行
def view(request, *args, **kwargs):# cls:Mylogin()------self对象self cls(**initkwargs)return self.dispatch(request, *args, **kwargs) # View类里的dispatchdef 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)else:handler self.http_method_not_allowedreturn handler(request, *args, **kwargs)from django.views import Viewclass MyLogin(View):http_method_names [get, post]def get(self, request):print(get...)self.index()return HttpResponse(get)def post(self, request):return HttpResponse(hello postman!!!)def index(self):pass
小结
url(r^login/, views.MyLogin.as_view()),
当我们启动Django项目时会自动触发路由中的方法调用 as_view 方法并自执行在执行后我们查看 as_view 方法的源码 发现 在依次给我们的对象赋值后最终返回了一个自执行的 dispatch 方法于是我们又去查看了 dispatch 方法 在 dispatch 内部 先是将请求方式转换并进行校验然后开始校验需要调用的方法的调用位置校验成功并拿到需要执行的方法执行在自己写的类中如果有相关的方法会首先调用我们重写的类方法并返回执行结果 如果自己的类里面没有该方法 会去自己的父类中调用 父类的方法 如果父类 以及 基类 都找不到则报错抛出异常