Reputation: 13
I use httpie to test my api,when I text
localhost:8000/users/
it show the user list,then i text
localhost:8000/users/jack/
it still show the user list,not the user detail,it's something wrong with my code?
url.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('rest_framework.urls',namespace='rest_framework')),
url(r'regist/', Regist.as_view()),
url(r'users/', UserList.as_view()),
url(r'users/(?P<username>[a-zA-Z0-9]+)/$', UserDetail.as_view()),
]
views.py
class UserDetail(generics.ListAPIView):
serializer_class= UserSeriallizer
def get_queryset(self):
username = self.kwargs['username']
user=User.objects.filter(username=username)
return user
class UserList(APIView):
def get(self, request):
users = User.objects.all()
serializer = UserSeriallizer(users, many=True)
return Response(serializer.data)
Upvotes: 0
Views: 45
Reputation: 8897
Problem in your urls, you need to close r'users/$
, because Django can't go further users/
without $
And why you use ListAPIView
for retrieving single object?
You need RetrieveAPIView
or RetrieveUpdateAPIView
if you want change the data. And change your view like so:
class UserDetail(RetrieveAPIView):
lookup_field = 'username'
queryset = User.objects.all()
You don't need get_queryset
at all
Upvotes: 1