{"id":265,"date":"2011-08-15T09:58:17","date_gmt":"2011-08-15T13:58:17","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=265"},"modified":"2011-11-13T21:24:17","modified_gmt":"2011-11-14T01:24:17","slug":"class-based-views-am-i-doing-it-right","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2011\/08\/class-based-views-am-i-doing-it-right\/","title":{"rendered":"Class-Based Views &#8211; Am I Doing It Right?"},"content":{"rendered":"<p>I thought this would be simpler than it turned out to be.  Here&#8217;s the &#8216;before&#8217; code:<\/p>\n<pre>def start(request):\r\n    ## clear out all session variables\r\n    request.session['username'] = None\r\n    \r\n    return render_to_response('start.html',\r\n                              context_instance=RequestContext(request) )<\/pre>\n<p>This view resets a session variable and sends out a template.  Not too tough.<\/p>\n<p>Next, is my first attempt to convert to a class-based view:<\/p>\n<pre>class StartView(TemplateView):\r\n    template_name = \"start.html\"\r\n    self.request.session['username'] = None<\/pre>\n<p>However, I quickly learned that &#8216;self&#8217; is not available here.  So, I picked a method, and overrode it to include my reset code:<\/p>\n<pre>class StartView(TemplateView):\r\n    template_name = \"start.html\"\r\n    \r\n    def render_to_response(self,context, **response_kwargs):\r\n        self.request.session['username'] = None\r\n        return super(StartView, self).render_to_response(context, **response_kwargs)<\/pre>\n<p>Sure, this works, but there is likely a better way.<\/p>\n<p>Please leave your suggestions in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought this would be simpler than it turned out to be. Here&#8217;s the &#8216;before&#8217; code: def start(request): ## clear out all session variables request.session[&#8216;username&#8217;] = None return render_to_response(&#8216;start.html&#8217;, context_instance=RequestContext(request) ) This view resets a session variable and sends out a template. Not too tough. Next, is my first attempt to convert to a class-based &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2011\/08\/class-based-views-am-i-doing-it-right\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Class-Based Views &#8211; Am I Doing It Right?&#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-265","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/265","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=265"}],"version-history":[{"count":4,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/265\/revisions"}],"predecessor-version":[{"id":288,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/265\/revisions\/288"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}