{"id":283,"date":"2011-11-13T21:20:27","date_gmt":"2011-11-14T01:20:27","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=283"},"modified":"2011-11-13T21:20:27","modified_gmt":"2011-11-14T01:20:27","slug":"more-on-class-based-views-redirect-if-data-missing","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2011\/11\/more-on-class-based-views-redirect-if-data-missing\/","title":{"rendered":"More on Class Based Views &#8211; Redirect If Data Missing"},"content":{"rendered":"<p>In this view, rather than raising a 404 if a piece of data is missing, I redirect back to a page where the user can specify the value.<\/p>\n<pre>def list_session(request):\r\n    event_id = request.session.get('event_id',None)\r\n    try:\r\n        event = Event.objects.get(pk=event_id)\r\n    except:  #No event - back to staff page to select\r\n        return HttpResponseRedirect(reverse('staff'))\r\n\r\n    queryset = B2CSession.objects.filter(event = event)\r\n\r\n    params = {'queryset': queryset,\r\n              'paginate_by': DEFAULT_PAGINATION,\r\n              'template_name': 'session_list.html',\r\n              'extra_context': {'event': event,\r\n                                'session': session}}\r\n    return object_list(request, **params)<\/pre>\n<p>I wanted to implement this same functionality using a class based view, so I looked into the available methods to override.  The <code>dispatch<\/code> method was really the only choice.  My solution:<\/p>\n<pre>def dispatch(self, request, *args, **kwargs):\r\n        ## If there is no event_id set in the session, return to staff page for a chance to select one\r\n        try:\r\n            self.event = Event.objects.get(pk=request.session.get('event_id',None))\r\n        except Event.DoesNotExist:\r\n            return HttpResponseRedirect(reverse('staff'))\r\n        ## Normal processing\r\n        return super(ListSessionView, self).dispatch(request, *args, **kwargs)  <\/pre>\n<p>What do you think?  Is this the best way to make this work? Leave your comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this view, rather than raising a 404 if a piece of data is missing, I redirect back to a page where the user can specify the value. def list_session(request): event_id = request.session.get(&#8216;event_id&#8217;,None) try: event = Event.objects.get(pk=event_id) except: #No event &#8211; back to staff page to select return HttpResponseRedirect(reverse(&#8216;staff&#8217;)) queryset = B2CSession.objects.filter(event = event) params &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2011\/11\/more-on-class-based-views-redirect-if-data-missing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;More on Class Based Views &#8211; Redirect If Data Missing&#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-283","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/283","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=283"}],"version-history":[{"count":2,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/283\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}