我正在对VIEW的post方法进行单元测试,并获得403错误和以下错误。
`def single_plate_pri(self):
u = User.objects.get(username = 'c269880')
self.client.force_authenticate(u)
data = copy.deepcopy(self.valid_pri_data)
factory = RequestFactory()
post_url = '/sequence-request/confirm/'
request = factory.post(post_url, data, user= u,
simulation_mode = True, enforce_csrf_checks=True)
request.user = User.objects.get(username='c269880')
view = csrf_exempt(SequencingRequestSpreadsheetView().as_view())
resp = view(request)
self.assertEqual(resp.status_code, 200)`
错误:
{'detail':ErrorDetail(string ='CSRF Failed:CSRF cookie not set ..', code ='permission_denied')}
我什至在视图上尝试过csrf,但是它没有用。
答案 0 :(得分:0)
将csrf_exempt添加到您的view.py
:
from django.views.decorators.csrf import csrf_exempt
不要使用这种类型的
@csrf_exempt
def single_plate_pri(self):
u = User.objects.get(username = 'c269880')
self.client.force_authenticate(u)
data = copy.deepcopy(self.valid_pri_data)
factory = RequestFactory()
post_url = '/sequence-request/confirm/'
request = factory.post(post_url, data, user= u,
simulation_mode = True, enforce_csrf_checks=True)
request.user = User.objects.get(username='c269880')
view = csrf_exempt(SequencingRequestSpreadsheetView().as_view())
resp = view(request)
self.assertEqual(resp.status_code, 200)