def test_post_request_for_api_view(self):
data = {
"email": self.user.email,
}
url = self.reverse('users:the_api', self.user.pk)
response = self.json_post(data, url=url)
self.mock.assert_called_once_with(self.user)
data2 = json.loads(response.content.decode('utf-8'))
self.assertEqual(data2, {
'booking_order': ['ABCDEFGHIJKL'],
'transaction_total': '20000.00'
})
urlpatterns = [
url(
regex=r'^$',
view=views.UserListView.as_view(),
name='list'
),
url(
regex=r'^~redirect/$',
view=views.UserRedirectView.as_view(),
name='redirect'
),
url(
regex=r'^(?P<username>[\w.@+-]+)/$',
view=views.UserDetailView.as_view(),
name='detail'
),
url(
regex=r'^~update/$',
view=views.UserUpdateView.as_view(),
name='update'
) ,
url(
regex=r'^the_api/$',
view = views.UserApiView.as_view(),
name='the_api'
),
class UserApiView(APIView):
authentication_classes = authentication.TokenAuthentication ###Am assuming you're authenticating via a token
def get(self, request):
"""
Get user based on username.
Am getting only the username since that's the only field used above.
:param request:
:param format:
:return:
"""
details = User.objects.all()
serializer = UserSerializer(details, many=True)
return Response(serializer.data )
def post(self, request, format=None):
"""
Create a new user instance
:param request:
:param format:
:return:
"""
serializer = UserSerializer(request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors)
尽管我理解Django中的反向是如何工作的,但我相信上面的测试通过,我只需要在我的中有一个 the_api 网址urls.py 。 然而,即使这样,测试也没有通过。 因此,我很想知道正确的做法是什么。 我如何让测试运行?
我不断得到的错误是:
django.urls.exceptions.NoReverseMatch:反向&#39; the_api&#39;参数&#39;(1,)&#39;和关键字参数&#39; {}&#39;未找到。尝试了1种模式:[&#39;用户:the_api / $&#39;]
答案 0 :(得分:0)
你在网址上接受了争论吗?查看代码,您应该在urls.py文件中输入以下内容:
url(r'the_api/(\d+)/$', your_view, name="the_api")
可能您应该粘贴您的网址条目以及相应的视图,以便我们更深入地了解问题。