Reputation: 3778
I have a django.views.generic.DetailView
-based class called LocationView
that is set up like this:
class LocationView(DetailView):
model = Location
pk_url_kwarg = 'location_id',
template_name = 'accounts/locations/view_location.html'
And the corresponding url definition:
url(
r'^accounts/(?P<account_id>\d+)/locations/(?P<location_id>\d+)/$'
LocationView.as_view(),
name='view_location',
)
When I try to access LocationView
in my browser, I get the following exception:
AttributeError: Generic detail view LocationView must be called with either an object pk or a slug.
After much digging, I found out that somewhere along the line, self.pk_url_kwarg
gets changed from 'location_id'
to ('location_id', )
, which causes the view to fail to retrieve the object's pk when it runs self.kwargs.get(self.pk_url_kwarg)
because none of the keys in self.kwargs
matches the modified pk_url_kwarg
value.
Why is this happening and how can I fix it?
django.VERSION == (1, 11, 'final', 0)
Upvotes: 0
Views: 44
Reputation: 53669
It's because you've set it to a tuple:
pk_url_kwarg = 'location_id',
Note the trailing comma. That is what turns a statement into a tuple. To fix it, simply remove the comma:
pk_url_kwarg = 'location_id'
Upvotes: 2