Django class based view with GET parameters

Viewed 4693

Using django rest framework I'm trying to create a view that allows to retrieve and update a single user but I don't understand how to do it. At the moment I don't care about permissions and authentication.

views.py

class UserDetailsView(RetrieveUpdateAPIView):
    def get_object(self, user_id):
        user = get_user_model().objects.get(pk=user_id)
        return user

urls.py

urlpatterns = [
    #rest of code
    url(r'^user/(?P<user_id>[0-9]+)/$', views.UserDetailsView.as_view(), name="profile"),
]

if I try to access localhost:8000/user/1 I get:

TypeError at /user/1/
get_object() missing 1 required positional argument: 'user_id'

Request Method:     GET
Request URL:    http://localhost:8000/user/1/
Django Version:     1.11.1
Exception Type:     TypeError
Exception Value:    

get_object() missing 1 required positional argument: 'user_id'

I am missing something because with function based views everything works just fine. For example:

views.py

def game(request, id_game):
    # rest of code

urls.py

 url(r'^games/(?P<id_game>[0-9]+)/$', views.game, name="game"),
3 Answers
Related