Python 405测试经过身份验证的django rest框架路由时出错
我正在用Python 405测试经过身份验证的django rest框架路由时出错,python,django,rest,testing,django-rest-framework,Python,Django,Rest,Testing,Django Rest Framework,我正在用APITestCase类测试CreateAPIView。作为匿名用户,一切正常,但当我以用户身份登录()时,会出现405HttpResponseNotAllowed异常。在通过django rest framework web前端以用户身份进行身份验证时,我能够成功地创建一个对象。我使用的是djangorestframework版本3.9.4和Django 1.11.29 以下是代码的主要部分,让我大致了解我在做什么: class SherdNoteCreate(CreateAPIVie
APITestCase
类测试CreateAPIView
。作为匿名用户,一切正常,但当我以用户身份登录()时,会出现405HttpResponseNotAllowed
异常。在通过django rest framework web前端以用户身份进行身份验证时,我能够成功地创建一个对象。我使用的是djangorestframework版本3.9.4和Django 1.11.29
以下是代码的主要部分,让我大致了解我在做什么:
class SherdNoteCreate(CreateAPIView):
serializer_class = SherdNoteSerializer
def post(self, request, *args, **kwargs):
data = request.data.copy()
data['asset'] = kwargs.get('asset_id')
serializer = SherdNoteSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class SherdNoteTests(APITestCase):
def test_create_sherdnote_on_own_asset(self):
# Removing this auth block allows the test to pass
self.u = UserFactory(username='test_user')
self.u.set_password('test')
self.u.save()
login = self.client.login(username='test_user', password='test')
assert(login is True)
asset = AssetFactory(primary_source='image', author=self.u)
url = reverse('sherdnote-create', kwargs={'asset_id': asset.pk})
data = {
'title': 'note title',
'body': 'note body'
}
response = self.client.post(url, data, format='json')
# This fails with a 405!
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
URL.py中的路由:
url(r'^(?P<asset_id>\d+)/sherdnote/create/$',
SherdNoteCreate.as_view(),
name='sherdnote-create'),
我一直很难找到发生这种情况的原因。有人有什么想法吗?好的,我的应用程序中有一些课程中间件干扰了我的API请求。解决方案是在发出请求之前制作一个示例课程,并让我的测试用户成为该课程的学生。这可能会有所帮助。看起来
url=reverse('sherdnote-create',kwargs={asset_id':asset.pk})存在一些问题
你能调试这一行,看看你的发帖请求是否发送到了正确的url。@Muhammaddnan发帖请求被发送到了正确的url:/asset/1/sherdnote/create/
你能使用邮递员或curl成功地发帖吗?405 HTTP响应状态代码表示服务器知道请求方法,但目标资源不支持该方法。@nnyby您可以尝试使用django TestCase而不是APITestCase,并查看输出是否更改。另外,尝试将视图中的post
方法替换为create
,它们应该是相同的。
<HttpResponseNotAllowed [GET, HEAD, OPTIONS] status_code=405, "text/html; charset=utf-8">
{'_headers': {'content-type': ('Content-Type', 'text/html; charset=utf-8'), 'allow': ('Allow', 'GET, HEAD, OPTIONS'), 'vary': ('Va
ry', 'Origin, Cookie'), 'x-frame-options': ('X-Frame-Options', 'SAMEORIGIN'), 'content-length': ('Content-Length', '0')}, '_closab
le_objects': [<WSGIRequest: POST '/asset/1/sherdnote/create/'>], '_handler_class': None, 'cookies': <SimpleCookie: >, 'closed': Tr
ue, '_reason_phrase': None, '_charset': None, '_container': [b''], 'wsgi_request': <WSGIRequest: POST '/asset/1/sherdnote/create/'
>, 'client': <rest_framework.test.APIClient object at 0x7f9880902f10>, 'request': {'PATH_INFO': '/asset/1/sherdnote/create/', 'REQ
UEST_METHOD': 'POST', 'SERVER_PORT': '80', 'wsgi.url_scheme': 'http', 'CONTENT_LENGTH': 41, 'CONTENT_TYPE': 'application/json; cha
rset=None', 'wsgi.input': <django.test.client.FakePayload object at 0x7f987e834790>, 'QUERY_STRING': ''}, 'templates': [], 'contex
t': None, 'json': <function curry.<locals>._curried at 0x7f988018e440>, 'resolver_match': <SimpleLazyObject: <function Client.requ
est.<locals>.<lambda> at 0x7f988018eef0>>, '_dont_enforce_csrf_checks': True}