python请求403错误

时间:2016-04-19 01:27:25

标签: python django

我正在获取序列化的模型对象并将它们作为json

返回

请求:

data={'Page':1}

r=requests.post('http://127.0.0.1:8000/GetTenders',data=data)

Django视图

def GetTenders(request):
    if request.is_ajax() or request.POST:
        page_num = int(request.POST.get('Page'))
        if page_num != 1:
            start = (page_num - 1) * 4
            objs = Tender.objects.all()[start:(start + 4)]
            return HttpResponse(serializers.serialize('json',objs), content_type='application/json')
        else:
            objs = Tender.objects.all()[:4]
            return HttpResponse(serializers.serialize('json', objs), content_type='application/json')
    else:
        raise Http404

url.py

url(r'^GetTenders$', views.GetTenders,name='gettenders'),

结果

>>>r
<Response [403]>
>>>r.url
u'http://127.0.0.1:8000/GetTenders'

由ajax调用的Django视图,但我想在使用

之前进行测试

解决方法是删除request.is_ajax()并放置装饰器 查看之前的@csrf_exempt

0 个答案:

没有答案