Django框架之视图函数(day74)
一 作业相关 urlpatterns = [ url(r'^$',views.book), #根路径,响应到指定视图函数: ..... url(r'',views.errors), #没有配置的路径,响应到错误的视图函数: ]二 虚拟环境创建方法 1 用pychanrm创建--->files-->newproject--->选择虚拟环境 2 settings-->project创建 3 用命令行创建,详见https://www.cnblogs.com/liuqingzheng/p/9508851.html三 django 2.0和django 1.0 路由层区别 1 url,re_path分组分出来的数据,是字符串***** 2 re_path:跟1.0的url用法相同 3 path:传的路径,是准确路径 4 5个转换器-->path('test/<path:year>', views.re_test), str,匹配除了路径分隔符(/)之外的非空字符串,这是默认的形式 int,匹配正整数,包含0。 slug,匹配字母、数字以及横杠、下划线组成的字符串。 uuid,匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00。 path,匹配任何非空字符串,包含了路径分隔符(/)(不能用?) 5 自定义转换器 1 定义一个类: class MyCon: regex = '[0-9]{4}' # 写一个正则表达式 def to_python(self, value): # 匹配出來的数据,会传到这里,retrun回去的,会被视图函数接收 return int(value) def to_url(self, value): # 反向解析用的 return '%04d' % value 2 from django.urls import register_converter register_converter(MyCon,'yyy') #重命名 3 path('test/<yyy:year>', views.re_test,name='test'), 6 补充: 在settings中如果设置: APPEND_SLASH=False 为假,不会加反斜杠四 视图层之HttpRequest对象 1 前台Post传过来的数据,包装到POST字典中 # request.POST 2 前台浏览器窗口里携带的数据,包装到GET字典中 # request.GET 3 前台请求的方式 # request.method 4 post提交的数据,body体的内容,前台会封装成:name=lqz&age=18&sex=1 # request.body 5 取出请求的路径,取不到数据部分 # print(request.path) 6 取出请求的路径,能取到数据部分 # print(request.get_full_path()) # print(request.META)五 视图层之HttpResponse对象 三件套:render,HttpResponse,redirect render函数: temp=Template('<h1>{ { user }}</h1>') con=Context({'user':'lqz'}) ret=temp.render(con) return HttpResponse(ret) #等效于上面四步:return render(request,'index.html')六 视图层之JsonResponse对象 1 导入:from django.http import JsonResponse 2 视图函数中: def test(request): import json # dic={'name':'lqz','age':18} ll = ['name', 'age'] # 把字典转换成json格式,返回到前台 # return HttpResponse(json.dumps(dic)) # 把列表转换成json格式,返回到前台 # return HttpResponse(json.dumps(ll)) # 把字典转换成json格式,返回到前台 # return JsonResponse(dic) # 报错,默认不支持列表形式 # return JsonResponse(ll) # 支持列表形式 return JsonResponse(ll,safe=False)七 CBV和FBV 1 CBV基于类的视图 1 路由层:url(r'^test/', views.Test.as_view()), 2 视图层 导入:from django.views import View 写一个类: class Test(View): def get(self, request): #一定要传request对象 return HttpResponse('get-test') def post(self, request): return HttpResponse('post-test') 2 FBV基于函数的视图 八 简单文件上传 1 前端部分: <form action="" method="post" enctype="multipart/form-data"> {#<form action="" method="post" enctype="application/x-www-form-urlencoded">#} <input type="file" name="myfile"> <input type="text" name="password"> <input type="submit" value="提交"></form> 2 后台部分: def fileupload(request): if request.method=='GET': return render(request,'fileupload.html') if request.method=='POST': print(request.FILES) print(type(request.FILES.get('myfile'))) myfile=request.FILES.get('myfile') # 从字典里根据名字,把文件取出来 from django.core.files.uploadedfile import InMemoryUploadedFile name=myfile.name # 文件名字 with open(name,'wb') as f: # for line in myfile.chunks(): for line in myfile: f.write(line) return HttpResponse('ok') 3 补充:编码方式multipart/form-data或者:application/x-www-form-urlencoded传的数据,都可以从POST中取出来九 作业: 1 手动创建虚拟环境--用命令 2 往前台返回user字典:[{name:lqz,age:18},{name:lqz2,age:18}] 3 写一个文件上传:如果文件名字重复,不要覆盖,并且放到项目根路径的media文件夹下