Django第六日
一 视图层之请求对~
view.py
def index(request):
'''
request:django封装的对象,它的类是WSGIRequest,它里面包含了所有http请求的东西
'''
print(request)
print(type(request))
# from django.core.handlers.wsgi import WSGIRequest
#######################1 复习
print(request.method)
print(request.GET)
print(request.POST)
########################2 新的操作,path,get_full_path,META,FIELS,body
# 自定制请求头
# 上传文件使用的编码方式是form-data,默认编码方式urlencoded
print(request.is_ajax()) # 是不是ajax请求
print(request.path) # 请求路径
print(request.get_full_path()) # 请求全路径,带数据
# print(request.body) # 请求体,二进制,如果传文件,这个报错
'''
使用form表单,默认情况下数据被转成name=lqz&password=123放到请求体中
request.POST其实是从body中取出bytes格式的,转成了字典
requet.GET其实是把路径中?后面的部分拆出来,转成了字典
'''
print(request.encoding) # 客户端向服务端传递时,使用的编码方法
print(request.META) # 重点,字典,一堆东西,请求用户的ip地址,请求头中数据,用户自定制请求头的数据
'''
把请求头的key值部分统一加HTTP_ 并且全部转成大写
'''
print(request.META['REMOTE_ADDR']) # 客户端的ip地址
print(request.FILES) # 客户端上传的文件
########################3 暂时不用关注(后面会讲)
print(request.COOKIES) # 空字典
print(request.session) # session对象
print(request.user) # 匿名用户
return HttpResponse('ok')
二 视图层之响应对象
重点:JsonResponse的使用(看源码)
三件套,复习
return HttpResponse('ok')
return render(request,'index.html',context={'name':'lqz','age':18})
return redirect('/home') # 重定向自己的地址,重定向第三方地址,经常跟反向解析一起使用
向客户端返回json格式数据,用json模块来写的笨方法
import json
res=json.dumps({'name':'刘清政','age':18},ensure_ascii=False) # 最后的参数用于使中文解码正确,复习
return HttpResponse(res)
django内置提供的JsonResponse
本质还是HttpResponse
return JsonResponse({'name':'刘清政','age':18},json_dumps_params={'ensure_ascii':False})
# safe,转换除字典以外的格式,需要safe=False,否则会报错
return JsonResponse([11,12,13,'lqz',[1,2,3],{'name':'lqz','age':19}],safe=False)
三 cbv和fbv
- CBV是基于类的视图(Class base view)
如之前学的全是FBV,写的是视图函数 - FBV则是基于函数的视图(Function base view)
写视图类cbv流程(还是写在views.py中)
- 写一个类,继承View
from django.views import View class Index(View): def get(self, request): # 当url匹配成功,get请求,会执行它 return HttpResponse('ok') def post(self,request): return HttpResponse('post')
- 配置路由
path('index/', views.Index.as_view()),
我们前期现在用的全是FBV,而后期的drf则全是CBV
文件上传
要注意html编码方式
form表单一定要规定编码属性为:enctype="multipart/form-data
<form action="/index/" method="post" enctype="multipart/form-data">
<p>用户名:<input type="text" name="name"></p>
<p>密码:<input type="password" name="password"></p>
<p><input type="file" name="myfile"></p>
<p><input type="submit" value="提交"></p>
</form>
views.py 中保存文件的代码
def index(request):
file=request.FILES.get('myfile')
# 打开一个空文件,写入
with open(file.name,'wb') as f:
for line in file.chunks(): # 要用chunks()方法使文件对象变为可迭代对象,才能遍历写入
f.write(line)
return HttpResponse('文件上传成功')
四 form表单,提交地址
action属性
- 不写,默认向当前地址发送请求
- 如
/index/
,向当前域(http://127.0.0.1:8000/)的/index/发送请求 - 如
http://127.0.0.1:8000/index/
,向具体的该地址发送请求(可以向第三方服务发送请求)
method属性
4. post
:发送post请求(默认编码情况下:以key=value&key=value
的形式拼到请求体中)
5. get
:发送get请求(以key=value&key=value
的形式拼到路径中)
<form action="/index/" method="post">
<p>用户名:<input type="text" name="name"></p>
<p>密码:<input type="text" name="password"></p>
<p><input type="submit" value="提交"></p>
</form>
顺便提一嘴之Pycharm的自动提示
from django.core.handlers.wsgi import WSGIRequest
# pycharm的自动提示
request=request # type: WSGIRequest # 左边这样的注释可以使 request. 后直接弹出相应的提示type
res = res # type:str # 如这样 res. 就会直接弹出字符串相关的方法