解决Django的POST请求接口返回403错误


解决办法:使用@csrf_exempt装饰器,避开CsrfViewMiddleware的检测 
示例:

from django.views.decorators.csrf import csrf_exempt 
from django.http import HttpResponse

import json


@csrf_exempt 
def my_view(request): 
    resp_dict = {
        'c': -1,
        'm': 'fail',
        'd': []
    }
    if request.method == 'POST':
        resp_dict['c'] = 0
        resp_dict['m'] = 'success'
    else:
        resp_dict['c'] = -2
        resp_dict['m'] = 'request method error'
    return HttpResponse(json.dumps(resp_dict), content_type="application/json")
阅读更多
换一批

没有更多推荐了,返回首页