内容回顾:
1.开发模式
- 普通开发方式(前后端放在一起写)
- 前后端分离
2.后端开发
为前端提供URL(API/接口的开发)
注:永远返回HttpResponse
3.Django FBV、CBV
FBV,function base view
def users(request):
user_list =['alex','oldboy']
return HttpResponse(json.dumps((user_list)))
CBV, class base view
路由:
url(r'^students/', views.StudentsView.as_view()),
视图:
from django.views import View
class StudentsView(View):
def get(self,request,*args,**kwargs):
return HttpResponse('GET')
def post(self,request,*args,**kwargs):
return HttpResponse('POST')
def put(self,request,*args,**kwargs):
return HttpResponse('PUT')
def delete(self,request,*args,**kwargs):
return HttpResponse('DELETE')
4.列表生成式
class Foo:
pass
class Bar:
pass
v = [item() for item in [Foo,Bar]]
v对象列表
5.面向对象
-封装
-对同一类方法封装在类中
class File:
文件增删改查方法
class DB:
数据库的方法
- 将数据封装在对象中
class FIle:
def __init__(self,a1,a2):
self.a1 = a1
self.xxx = a2
def get:...
obj1 = File(123,456)
今日概要:
1.restful规范(建议)
2.django rest framework框架
内容详细:
0.FBV、CBV
CBV,基于反射实现根据请求方式不同,执行不同的方法。
原理:
a.路由
url->view方法->dispatch方法(反射执行其他:GET/POST/DELETE/PUT)
面试题:
1.django中间件
-process_request
-process_view
-process_response
-process_exception
-process_render_template
2.使用中间件做过什么?
-权限、
-用户登录验证
-django的csrf是如何实现?
process_view方法
-检查视图是否被@csrf_exempt(免除csrf认证)
-去请求或cookie中获取token
3.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',#全栈使用csrf认证
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt #该函数无需认证
def users(request):
user_list =['alex','oldboy']
return HttpResponse(json.dumps((user_list)))
CBV小知识:
-method_decorator(csrf_exempt)
-在dispatch方法中(单独方法无效)
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
@csrf_exempt
def users(request):
user_list =['alex','oldboy']
return HttpResponse(json.dumps((user_list)))
from django.views import View
class StudentsView(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(StudentsView,self).dispatch(request,*args,**kwargs)
def get(self,request,*args,**kwargs):
return HttpResponse('GET')
def post(self,request,*args,**kwargs):
return HttpResponse('POST')
def put(self,request,*args,**kwargs):
return HttpResponse('PUT')
def delete(self,request,*args,**kwargs):
return HttpResponse('DELETE')
总结:
- 本质:基于反射来实现
- 流程:路由,view,dispatch(反射)
- 取消csrf认证(装饰器要加到dispatch方法上且method_decorator装饰)
扩展:
-csrf
-基于中间件的process_viewfang
1.restful规范(建议)
a.接口开发
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^get_order/',views.get_order),
url(r'^add_order/',views.add_order),
url(r'^del_order/',views.del_order),
url(r'^update_order/',views.update_order)
]
def get_order(request):
return HttpResponse('')
def add_order(request):
return HttpResponse('')
def del_order(request):
return HttpResponse('')
def update_order(request):
return HttpResponse('')
b.restful规范(建议)
1.根据method不同做不同的操作,示例
基于FBV:
url(r'^order/',views.order)
def order(request):
if request.method == 'GET':
return HttpResponse('获取订单')
elif request.method == 'POST':
return HttpResponse('创建订单')
elif request.method == 'PUT':
return HttpResponse('更新订单')
elif request.method == 'DELETE':
return HttpResponse('删除订单')
基于CBV:
urlpatterns = [
url(r'^order/',views.OrderView.as_view())
]
class OrderView(View):
def get(self,request,*args,**kwargs):
return HttpResponse('获取订单')
def post(self,request,*args,**kwargs):
return HttpResponse('创建订单')
def put(self,request,*args,**kwargs):
return HttpResponse('更新订单')
def delete(self,request,*args,**kwargs):
return HttpResponse('删除订单')
c.谈谈自己对restful api规范的认识
1.本质上就是一个规范
2.在URL体现出对API的一些操作
2.django rest framework框架
pip3 install djangorestframework
a.认证
-仅使用
-源码流程
dispatch ...