【发布时间】:2018-09-23 13:22:15
【问题描述】:
使用if request.method=='POST': 或if request.method=='GET': 启动我的每一个视图函数是否有好处?还是我只是添加不必要的代码行?
我遵循了几个示例,其中 Ajax 的视图都在检查 HTTP 是否是使用 GET 生成的。
例如,它能否阻止 DDOS 锁定 POST 方法并使用 GET 攻击它?或者,更实际地,防止 API 使用者在应该 PUT 或 POST 时错误地修补?
def employee_delete(request, uid):
if request.method == 'DELETE':
def employee_detail(request, uid):
if request.method == 'GET':
def employee_create(request):
if request.method == 'POST':
def employee_update(request, uid):
if request.method == 'PUT':
【问题讨论】:
-
你可以使用
@require_http_method(..)装饰器docs.djangoproject.com/en/2.1/topics/http/decorators/… -
但这可能是个好主意的原因是因为
GET不应该有副作用,如果“互联网蜘蛛”(机器人)或用户不小心访问了@987654327 @链接通过GET,员工被删除。
标签: django rest django-rest-framework django-views api-design