Shiva Krishna Bavandla
Shiva Krishna Bavandla

Reputation: 26728

request is not sending to correct view in django urls

I am using Django rest framework for designing API, and I have below code

urls.py

from .views import UserView, UserDetails

urlpatterns = [
    url(r'^user/', UserView.as_view(), name = 'users'),
    url(r'^user/(?P<user_id>[0-9]+)/', UserDetails.as_view(), name = 'users_detail'),
]

views.py

from rest_framework.decorators import api_view
from rest_framework import permissions


class UserView(APIView):
    def get(self, request, format=None):
        print "I am in userview !!"
        .....
        .....
        return Response(users.data)

    def post(self, request, format=None):
        .....
        .....
        return Response(data)

class UserDetails(APIView):
    permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        import ipdb; ipdb.set_trace()
        return Response('OK')

And the endpoints that I am trying are below

http://localhost:8000/api/user/
http://localhost:8000/api/user/1/

The problem what I am having is both the above URL requests are going to same UserView class, but actually

http://localhost:8000/api/user/ should go to UserView class which is correct and happening now, and http://localhost:8000/api/user/1/ should go to UserDetails class which is not happening right now and the request was still going to 'UserView' class and I don't know why, can anyone please let me know what's wrong in my code?

Upvotes: 0

Views: 104

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599946

You need to terminate your url patterns.

url(r'^user/$', ...),
url(r'^user/(?P<user_id>[0-9]+)/$', ...),

Upvotes: 3

Related Questions