一、FBV(function base views)html
在视图里使用函数处理请求。django
url:
re_path('fbv', views.fbv),
# url(r'^fbv', views.fbv),cookie
func:
def fbv(requset):
return render(requset,'fbv_Cbv.html')函数
二、CBV (class base views)post
url:url
re_path('cbv', views.Cbv.as_view()),
# url(r'^cbv', views.Cbv.as_view()),spa
class:orm
from django.views import View
#导入View模块
class Home(View): #使用类处理须要继承View(view是Home的父类,Home是子类)
def get(self,request): #自动识别,若是用户请求方式:get,那么自动调用该方法执行结果
print(request.method)
return render(request, 'home.html')
def post(self,request):
print(request.method)#自动识别,若是用户请求方式:post,那么自动调用该方法执行结果
return render(request, 'home.html')
htm
三、FBV+CBV 获取数据、返回数据的命令参数继承
A.获取数据的几种方式
request.method
request.GET
request.POST
request.FILES
request.path_info
request.COOKIES
reqeust.body #全部内容的原生数据
B.获取checkbox等多选的内容
request.POST.getlist()
request.GET.getlist()
#因此分红两类
request.body #用户请求的原生数据,以字符串形式存放
request.PUT
request.DELECT
request.MOVE
#django对上面3种没有作处理,因此咱们操做以上3种的时候就须要经过request.body获取原生数据:字符串形式
request.Meta
#出了数据外,请求头相关的信息,如:判断用户是PC端仍是移动端
request.method(POST、GET、PUT..)
request.path_info
request.COOKIES
C.接收上传文件,注意:模板文件html的form标签必须作特殊设置:enctype="multipart/form-data"
obj = request.FILES.get('file')
obj.name #取该文件名
obj.size #取该文件的字节大小
obj.chunks #取该文件的块
file_path = os.path.join('%s\\upload'%os.path.dirname(os.path.abspath(__file__)), obj.name) #当前文件的目录下的upload目录和接收文件名进行拼接成文件路径
f = open(obj.name,mode= 'wb') #若是该路径文件存在就打开,不存在就建立
for item in obj.chunks():
f.write(item) #写入文件
f.close() #关闭文件
D、返回给用户的几种方式
return render(request,'模板路径.html',{'list':[1,2,3,4],'dict': {'k1':'v1','k2':'v2'} }) #返回给用户指定通过模板渲染后的html
retune redirect(’url路径’) #跳转到指定url
retune HttpResponse(‘字符串’) #直接返回给用户字符串
response = HttpResponse(a)
response.set_cookie(‘key’:’value’) #设置客户端cookie
response[‘name’] = ‘bur’ #设置客户端响应头
return response
四、FBV+CBV 添加验证装饰器
A、FBV添加装饰器
def auth(func):
def deco(request, *args, **kwargs):
u = request.get_signed_cookie('username', salt='user', default=None)
if not u:
return render(request, 'login.html')
return func(request, *args, **kwargs)
return deco
@auth
def index(request):
u = request.get_signed_cookie('username', salt='user', default=None)
return render(request, 'index.html', {'user': u})
@auth
def detail(request):
u = request.get_signed_cookie('username', salt='user', default=None)
return render(request, 'detail.html', {'user': u})
访问index/detail时,调用auth装饰器,若是验证成功,则执行index/detail(return func(request, *args, **kwargs)语句起的做用);
不然跳转到login.html
B、CBV方式添加装饰器:经过django自带的装饰器method_decorator 的@method_decorator(cookie)来实现
from django.utils.decorators import method_decorator
from django import views
# @method_decorator(cookie,name='dispatch') # dispatch的便捷写法
class CBVtest(views.View):
@method_decorator(cookie) # 给dispatch方法添加装饰器,那么下面全部的get,post都会添加
def dispatch(self, request, *args, **kwargs):
return super(CBVtest, self).dispatch(request, *args, **kwargs)
# @method_decorator(cookie) # 单独添加
def get(self, request):
u = request.get_signed_cookie('username', salt='user', default=None)
return render(request, 'houtai.html', {'user': u})
def post(self, request):
return HttpResponse('post ok')