{"id":289,"date":"2011-11-14T16:34:52","date_gmt":"2011-11-14T20:34:52","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=289"},"modified":"2011-11-14T22:48:23","modified_gmt":"2011-11-15T02:48:23","slug":"class-based-views-detailview-example","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2011\/11\/class-based-views-detailview-example\/","title":{"rendered":"Class Based Views &#8211; DetailView Example"},"content":{"rendered":"<p>This is a simple example that shows how to convert the <code>object_detail<\/code> generic view to the class based <code>DetailView<\/code>.<\/p>\n<p>Before:<\/p>\n<pre>@permission_required('b2c.view_b2ctrack')\r\ndef view_track(request,id):\r\n    return object_detail(request,\r\n                         queryset=B2CTrack.objects.all(),\r\n                         object_id=id,\r\n                         template_name='track_detail.html')<\/pre>\n<p>After:<\/p>\n<pre>class TrackDetailView(DetailView):\r\n    template_name = 'track_detail.html'\r\n    model = B2CTrack\r\n    \r\n    ## Override dispatch to apply the permission decorator\r\n    @method_decorator(permission_required('b2c.view_b2ctrack'))\r\n    def dispatch(self, *args, **kwargs):\r\n        return super(TrackDetailView, self).dispatch(*args, **kwargs)  <\/pre>\n<p>Only odd thing I encountered was that the new View wants to see the ID of the object named &#8216;pk&#8217;, instead of &#8216;id&#8217; that I was using.  The DEV branch of Django includes the <code>pk_url_kwarg<\/code> variable that allows a rename, but it isn&#8217;t available in v1.3.<\/p>\n<p>Note that I could have used <code>queryset<\/code> instead of <code>model<\/code> to specify the data source.  Either will give the same result in this example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple example that shows how to convert the object_detail generic view to the class based DetailView. Before: @permission_required(&#8216;b2c.view_b2ctrack&#8217;) def view_track(request,id): return object_detail(request, queryset=B2CTrack.objects.all(), object_id=id, template_name=&#8217;track_detail.html&#8217;) After: class TrackDetailView(DetailView): template_name = &#8216;track_detail.html&#8217; model = B2CTrack ## Override dispatch to apply the permission decorator @method_decorator(permission_required(&#8216;b2c.view_b2ctrack&#8217;)) def dispatch(self, *args, **kwargs): return super(TrackDetailView, self).dispatch(*args, **kwargs) Only &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2011\/11\/class-based-views-detailview-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Class Based Views &#8211; DetailView Example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":4,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":297,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/289\/revisions\/297"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}