Skip to content Skip to sidebar Skip to footer

Generic Detail View Userprofiledetailview Must Be Called With Either An Object Pk Or A Slug In The Urlconf

Well I am facing error and it's now its been two days to this question and still stuck on this mistake, anybody can help and able to fix this. I am new in Django and need help. I s

Solution 1:

Problem

  1. str type used instead of slug type in urls.py
  2. UserProfileDetailView doesn't specify your custom slug_url_kwarg and slug_field
  3. UserProfileDetailView specifies UserProfile model but UserProfile model doesn't have attribute or method username, which is on `User table.

Solution

Reading Django's DetailView code, you'll find that the following is necessary to get your code working properly.

Update class attributes

views.py

classUserProfileDetailView(DetailView):
    slug_url_kwarg ="username"
    slug_field = "username"

Update UserProfile model OR Update UserProfileDetailView.get_slug_field

UserProfile is the lookup table defined for UserProfileDetailView and get_slug_fieldmethod, which readsslug_fieldproperty on the UserProfileDetailView doesn't support dot syntax method traversal (ie:user.username`). Thus either:

  1. UserProfile model must have reference to username or;
  2. get_slug_field must explicitly define slug field or;
  3. get_slug_field must add functionality for dot syntax method traversal

models.py

classUserProfile(models.model):
    ...
    
    @propertydefusername(self):
        self.user.username 

OR

classUserProfileDetailView(DetailView):
    ...

    defget_slug_field(self):
        self.user.username

Update username type in urls.py

Helpful, but not necessary.

urls.py

path('<slug:username>/', UserProfileDetailView.as_view(), name = 'detail'),

References

Django Detail View get_slug_field (Github): https://github.com/django/django/blob/master/django/views/generic/detail.py#L78-L80

Solution 2:

you need to add the user pk or slug in the url so that django can retrieve the user using this pk or slug so edit the url to be like this

path('<slug:username>/',UserProfileDetailView.as_view(),name = 'detail'),

but make sure that your slug equal the username of the user , to do this override the save method in your model to be like this

defsave(self, *args, **kwargs): 
    self.slug = self.username
    super(Your_Model_Name, self).save(*args, **kwargs)  

make sure to change 'Your_Model_Name' with your model class name

Solution 3:

Set slug_url_kwarg--(Django Doc) and slug_field--(Django Doc) attributes on your view class

classUserProfileDetailView(DetailView):
    slug_url_kwarg = "username" # this the `argument` in the URL conf
    slug_field = "your_model_field" # thisis the model field name.

    # Rest of your code

Post a Comment for "Generic Detail View Userprofiledetailview Must Be Called With Either An Object Pk Or A Slug In The Urlconf"