- 开发模式
- 普通开发模式(前后端放一起)
- 前后端分离
- 后端开发
为前端提供URL(API/接口的开发)
- 后端开发
- Django中的 FBV和 CBV
- FBV:function base view 基于函数的视图 view.py
def users(request): user_list = ['xxx', 'yyy'] return HttpResponse(json.dumps((user_list), )) - CBV:class base view 基于类的视图 view.py
注意:使用CBV,则在 url.py 要写入from django.views import View class StudentsView(View): def get(self, requeset, *args, **kwargs): return HttpResponse('GET') def post(self, requeset, *args, **kwargs): return HttpResponse('POST') def put(self, requeset, *args, **kwargs): return HttpResponse('PUT') def delete(self, requeset, *args, **kwargs): return HttpResponse('DELETE')类名.as_view()这种方式from django.config.urls import url from app01 import views urlpatterns = [ url('users/', views.users), url('students/'), views.StudentView.as_view()), ]
- FBV:function base view 基于函数的视图 view.py
- 若有相同功能的可以写在基类中:
from django.views import View class MyBaseView(object): def dispatch(self, request, *args, **kwargs): print('before') ret = super(MyBaseView, self).dispatch(request, *args, **kwargs) print('after') return ret class StudentsView(MyBaseView, view): def get(self, request, *args, **kwargs): print('get方法') return HttpResponse('GET')图片.png
总结:
默认是开启全站csrf认证的,
在FBV模式中,
方案一,注释掉django.middleware.csrf.CsrfViewMiddleware中间件;
方案二,在views.py中导入from Django.decorators.csrf import csrf_exempt, csrf_protect然后添加装饰器@csrf_exempt不使用认证,添加@csrf_protect使用认证
在CBV模式中,
方案一:导入from django.utils.decorators import method_decorator,from Django.decorators.csrf import csrf_exempt, csrf_protect,在方法上添加装饰器@method_decorator(csrf_exempt)不使用认证,加装饰器@method_decorator(csrf_protect)使用认证
方案二:在类中添加@method_decorator(csrf_exempt, name='dispatch')
from django.views import View
class MyBaseView(object):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
print('before')
ret = super(MyBaseView, self).dispatch(request, *args, **kwargs)
print('after')
return ret
# @method_decorator(csrf_exempt, name='dispatch') # 方案二
class StudentsView(MyBaseView, view):
def get(self, request, *args, **kwargs):
print('get方法')
return HttpResponse('GET')
- restful 规范
- 1.根据method的不同做不同的操作,只写一个URL
- 2.推荐使用HTTPS
- 3.命名规范:www.xxxx.com/api/v1/名词名称
- 4.方法method:
- GET :从服务器取出资源(一项或多项)
- POST :在服务器新建一个资源
- PUT :在服务器更新资源(客户端提供改变后的完整资源)
- PATCH :在服务器更新资源(客户端提供改变的属性)
- DELETE :从服务器删除资源
- 过滤
- https://api.example.com/v1/zoos?limit=10:指定返回记录的数量
- https://api.example.com/v1/zoos?offset=10:指定返回记录的开始位置
- https://api.example.com/v1/zoos?page=2&per_page=100:指定第几页,以及每页的记录数
- https://api.example.com/v1/zoos?sortby=name&order=asc:指定返回结果按照哪个属性排序,以及排序顺序
- https://api.example.com/v1/zoos?animal_type_id=1:指定筛选条件
- 状态码
- 200 OK - [GET]:服务器成功返回用户请求的数据,该操作是幂等的(Idempotent)。
- 201 CREATED - [POST/PUT/PATCH]:用户新建或修改数据成功。
- 202 Accepted - [*]:表示一个请求已经进入后台排队(异步任务)
- 204 NO CONTENT - [DELETE]:用户删除数据成功。
- 400 INVALID REQUEST - [POST/PUT/PATCH]:用户发出的请求有错误,服务器没有进行新建或修改数据的操作,该操作是幂等的。
- 401 Unauthorized - [*]:表示用户没有权限(令牌、用户名、密码错误)。
- 403 Forbidden - [*] 表示用户得到授权(与401错误相对),但是访问是被禁止的。
- 404 NOT FOUND - [*]:用户发出的请求针对的是不存在的记录,服务器没有进行操作,该操作是幂等的。
- 406 Not Acceptable - [GET]:用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)。
- 410 Gone -[GET]:用户请求的资源被永久删除,且不会再得到的。
- 422 Unprocesable entity - [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误。
- 500 INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功。
- 错误处理,状态码是4xx时,应返回错误信息,error当做key。
{ error: "Invalid API key" } - 返回结果,针对不同操作,服务器向用户返回的结果应该符合以下规范。
- GET /xxx:返回资源对象的列表(数组)
- GET /xxx/id:返回单个资源对象
- POST /xxx:返回新生成的资源对象
- PUT /xxx/id:返回完整的资源对象
- PATCH /xxx/id:返回完整的资源对象
- DELETE /xxx/id:返回一个空文档
- Hypermedia API,RESTful API最好做到Hypermedia,即返回结果中提供链接,连向其他API方法,使得用户不查文档,也知道下一步应该做什么。
{"link": { "rel": "collection https://www.example.com/zoos", "href": "https://api.example.com/zoos", "title": "List of zoos", "type": "application/vnd.yourformat+json" }}
