[Answer]-Accessing URL variables in Django 1.5 class-based view

1👍

I’m not sure where you got the idea that pk is the new name for object_id, nor why you think that you need to set it to a value per request. The point of the class-level attributes in class-based views is that they are set per view class, not per instance: they refer to the place the view will go to look up the value, not the actual value itself.

Your first mistake is that the equivalent of the old object-detail view is, not surprisingly, DetailView, not ListView. As the documentation shows, ListView gets the ability to show an object detail via its inheritance from the SingleObjectMixin. That mixin expects a class-level attribute called pk_url_kwarg, which is the name of the argument captured from the URL which identifies the object’s PK: in your case, this is the string 'stockcode'. The instance itself takes care of looking up that value in any particular request, you don’t need to do it.

Leave a comment