#28943: Avoid the need to call get_context_data() in TemplateView subclasses
-------------------------------------+-------------------------------------
     Reporter:  James Pic            |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Generic views        |                  Version:  2.0
     Severity:  Normal               |               Resolution:
                                     |  worksforme
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by James Pic):

 Yes, returning super().get() allows to bypass the last level of get()
 override. I try to demonstrate the issue with this script, perhaps it will
 make more sense.

 {{{
 class TemplateView(object):
     def get(self):
         print('render to response()')


 class YourBaseView(TemplateView):
     def get(self):
         print('generate token()')
         return super().get()


 class YourView(YourBaseView):
     def post(self):
         # if not dosomething(): return bad request
         return super().get()


 YourView().post()
 }}}

 This will print both 'generate token()' and 'render to response()'. If I
 only want to print 'render to response()', then this works:

 {{{
 class YourView(YourBaseView):
     def post(self):
         # if not dosomething(): return bad request
         return TemplateView.get(self)
 }}}

 But probably it would make sense to just move the render to response
 outside get() as such:

 {{{
 class TemplateView(object):
     def render_to_response(self):
         print('render to response()')

     def get(self):
         return self.render_to_response()


 class YourBaseView(TemplateView):
     def get(self):
         print('generate token()')
         return super().get()


 class YourView(YourBaseView):
     def post(self):
         # if not dosomething(): return bad request
         return self.render_to_response()
 }}}

 For me it seems like the last solution is best OOP but perhaps i'm
 mistaking again.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28943#comment:15>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.22c87d2e0ac3258a991cb85831056917%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to