self.assertEqual(response.status_code, status.HTTP_200_OK) AssertionError: 404 != 200
self.assertEqual(response.status_code, status.HTTP_200_OK) AssertionError: 404 != 200
这是错误来自
def test_single_status_retrieve(self):
serializer_data = ProfileStatusSerializer(instance=self.status).data
response = self.client.get(reverse("status-detail", kwargs={"pk": 1}))
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = json.loads(response.content)
self.assertEqual(serializer_data, response_data)
似乎错误来自其他地方。
这是我的设置
def setUp(self):
self.user = User.objects.create_user(username="test2",
password="change123",
)
self.status = ProfileStatus.objects.create(user_profile=self.user.profile,
status_content="status test")
self.token = Token.objects.create(user=self.user)
self.api_authentication()
这里是urls.py文件
from django.db import router
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from profiles.api.views import (AvatarUpdateView,
ProfileViewSet,
ProfileStatusViewSet,
)
router = DefaultRouter()
router.register(r"profiles", ProfileViewSet)
router.register(r"status", ProfileStatusViewSet, basename="status")
urlpatterns = [
path("", include(router.urls)),
path("avatar/", AvatarUpdateView.as_view(), name="avatar-update"),
]
这是我运行测试时得到的
(ProjectName) bash-3.2$ python3 manage.py test Creating test database for alias 'default'... System check identified no issues (0
silenced). F.........
================================================================= FAIL: test_single_status_retrieve
(profiles.tests.ProfileStatusViewSetTestCase)
---------------------------------------------------------------------- Traceback (most recent call last): File
"path.../ProjectName/src/profiles/tests.py", line 101, in
test_single_status_retrieve
self.assertEqual(response.status_code, status.HTTP_200_OK) AssertionError: 404 != 200
---------------------------------------------------------------------- Ran 10 tests in 1.362s
FAILED (failures=1) Destroying test database for alias 'default'...
不要忘记 change/create/delete
请求的 drf 中的 headers。
.
此外,您需要在 url.py 中添加 app_name: your_app_name
并将其放在 reverse() url.
中
像这样。
获取请求的 Ulr:reverse('your_app_name:your_router_basename-list')
Url 对于 delte/change 请求:reverse('your_app_name:your_router_basename-detail')
不要忘记在 response
中您需要放置:1- url、2- 数据和 3- JSON 格式。在 create/delete/change 个请求的情况下使用 headers。
试试这个方法:
def test_single_status_retrieve(self):
serializer_data = ProfileStatusSerializer(instance=self.status).data
# HERE
response = self.client.get(reverse("your_app_name:profile-detail", kwargs={"pk": 1}), serializer_data, headers={'content-type': 'application/json'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = json.loads(response.content)
self.assertEqual(serializer_data, response_data)
问题出在这个文件中
Views.py 在文件夹 profiles/api
from rest_framework import generics, mixins, viewsets
from rest_framework.filters import SearchFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from profiles.api.permissions import IsOwnProfileOrReadOnly, IsOwnerOrReadOnly
from profiles.api.serializers import (ProfileAvatarSerializer,
ProfileSerializer,
ProfileStatusSerializer,
)
from profiles.models import Profile, ProfileStatus
class AvatarUpdateView(generics.UpdateAPIView):
serializer_class = ProfileAvatarSerializer
permission_classes = [IsAuthenticated]
def get_object(self):
profile_object = self.request.user.profile
return profile_object
class ProfileViewSet(mixins.UpdateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated, IsOwnProfileOrReadOnly]
filter_backends = [SearchFilter]
search_fields = ["city"]
class ProfileStatusViewSet(ModelViewSet):
serializer_class = ProfileStatusSerializer
permission_classes = [IsAuthenticated, IsOwnerOrReadOnly]
def get_queryset(self):
queryset = ProfileStatus.objects.all()
username = self.request.query_params.get("username", None)
if username is not None:
queryset = queryset.filter(user_profile__user__username=username)
return queryset
def perform_create(self, serializer):
user_profile = self.request.user.profile
serializer.save(user_profile=user_profile)
改为
from rest_framework import generics, mixins, viewsets
from rest_framework.filters import SearchFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from profiles.api.permissions import IsOwnProfileOrReadOnly, IsOwnerOrReadOnly
from profiles.api.serializers import (ProfileAvatarSerializer,
ProfileSerializer,
ProfileStatusSerializer,
)
from profiles.models import Profile, ProfileStatus
class AvatarUpdateView(generics.UpdateAPIView):
serializer_class = ProfileAvatarSerializer
permission_classes = [IsAuthenticated]
def get_object(self):
profile_object = self.request.user.profile
return profile_object
class ProfileViewSet(mixins.UpdateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated, IsOwnProfileOrReadOnly]
filter_backends = [SearchFilter]
search_fields = ["city"]
class ProfileStatusViewSet(ModelViewSet):
serializer_class = ProfileStatusSerializer
permission_classes = [IsAuthenticated, IsOwnerOrReadOnly]
def get_queryset(self):
queryset = ProfileStatus.objects.all()
username = self.request.query_params.get("username", None)
if username is not None:
queryset = queryset.filter(user_profile__user__username=username)
return queryset
def perform_create(self, serializer):
user_profile = self.request.user.profile
serializer.save(user_profile=user_profile)
很难发现,提示看一下 def get_queryset(self):
中的 return queryset
这是错误来自
def test_single_status_retrieve(self):
serializer_data = ProfileStatusSerializer(instance=self.status).data
response = self.client.get(reverse("status-detail", kwargs={"pk": 1}))
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = json.loads(response.content)
self.assertEqual(serializer_data, response_data)
似乎错误来自其他地方。
这是我的设置
def setUp(self):
self.user = User.objects.create_user(username="test2",
password="change123",
)
self.status = ProfileStatus.objects.create(user_profile=self.user.profile,
status_content="status test")
self.token = Token.objects.create(user=self.user)
self.api_authentication()
这里是urls.py文件
from django.db import router
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from profiles.api.views import (AvatarUpdateView,
ProfileViewSet,
ProfileStatusViewSet,
)
router = DefaultRouter()
router.register(r"profiles", ProfileViewSet)
router.register(r"status", ProfileStatusViewSet, basename="status")
urlpatterns = [
path("", include(router.urls)),
path("avatar/", AvatarUpdateView.as_view(), name="avatar-update"),
]
这是我运行测试时得到的
(ProjectName) bash-3.2$ python3 manage.py test Creating test database for alias 'default'... System check identified no issues (0 silenced). F......... ================================================================= FAIL: test_single_status_retrieve (profiles.tests.ProfileStatusViewSetTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "path.../ProjectName/src/profiles/tests.py", line 101, in test_single_status_retrieve self.assertEqual(response.status_code, status.HTTP_200_OK) AssertionError: 404 != 200
---------------------------------------------------------------------- Ran 10 tests in 1.362s
FAILED (failures=1) Destroying test database for alias 'default'...
不要忘记 change/create/delete
请求的 drf 中的 headers。
.
此外,您需要在 url.py 中添加 app_name: your_app_name
并将其放在 reverse() url.
像这样。
获取请求的 Ulr:
reverse('your_app_name:your_router_basename-list')
Url 对于 delte/change 请求:
reverse('your_app_name:your_router_basename-detail')
不要忘记在 response
中您需要放置:1- url、2- 数据和 3- JSON 格式。在 create/delete/change 个请求的情况下使用 headers。
试试这个方法:
def test_single_status_retrieve(self):
serializer_data = ProfileStatusSerializer(instance=self.status).data
# HERE
response = self.client.get(reverse("your_app_name:profile-detail", kwargs={"pk": 1}), serializer_data, headers={'content-type': 'application/json'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = json.loads(response.content)
self.assertEqual(serializer_data, response_data)
问题出在这个文件中
Views.py 在文件夹 profiles/api
from rest_framework import generics, mixins, viewsets
from rest_framework.filters import SearchFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from profiles.api.permissions import IsOwnProfileOrReadOnly, IsOwnerOrReadOnly
from profiles.api.serializers import (ProfileAvatarSerializer,
ProfileSerializer,
ProfileStatusSerializer,
)
from profiles.models import Profile, ProfileStatus
class AvatarUpdateView(generics.UpdateAPIView):
serializer_class = ProfileAvatarSerializer
permission_classes = [IsAuthenticated]
def get_object(self):
profile_object = self.request.user.profile
return profile_object
class ProfileViewSet(mixins.UpdateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated, IsOwnProfileOrReadOnly]
filter_backends = [SearchFilter]
search_fields = ["city"]
class ProfileStatusViewSet(ModelViewSet):
serializer_class = ProfileStatusSerializer
permission_classes = [IsAuthenticated, IsOwnerOrReadOnly]
def get_queryset(self):
queryset = ProfileStatus.objects.all()
username = self.request.query_params.get("username", None)
if username is not None:
queryset = queryset.filter(user_profile__user__username=username)
return queryset
def perform_create(self, serializer):
user_profile = self.request.user.profile
serializer.save(user_profile=user_profile)
改为
from rest_framework import generics, mixins, viewsets
from rest_framework.filters import SearchFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from profiles.api.permissions import IsOwnProfileOrReadOnly, IsOwnerOrReadOnly
from profiles.api.serializers import (ProfileAvatarSerializer,
ProfileSerializer,
ProfileStatusSerializer,
)
from profiles.models import Profile, ProfileStatus
class AvatarUpdateView(generics.UpdateAPIView):
serializer_class = ProfileAvatarSerializer
permission_classes = [IsAuthenticated]
def get_object(self):
profile_object = self.request.user.profile
return profile_object
class ProfileViewSet(mixins.UpdateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated, IsOwnProfileOrReadOnly]
filter_backends = [SearchFilter]
search_fields = ["city"]
class ProfileStatusViewSet(ModelViewSet):
serializer_class = ProfileStatusSerializer
permission_classes = [IsAuthenticated, IsOwnerOrReadOnly]
def get_queryset(self):
queryset = ProfileStatus.objects.all()
username = self.request.query_params.get("username", None)
if username is not None:
queryset = queryset.filter(user_profile__user__username=username)
return queryset
def perform_create(self, serializer):
user_profile = self.request.user.profile
serializer.save(user_profile=user_profile)
很难发现,提示看一下 def get_queryset(self):
return queryset