Changes between Version 1 and Version 2 of Ticket #28943, comment 15


Ignore:
Timestamp:
Mar 6, 2018, 6:49:42 PM (7 years ago)
Author:
James Pic

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #28943, comment 15

    v1 v2  
    1 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.
    2 
    3 {{{
    4 class TemplateView(object):
    5     def get(self):
    6         print('render to response()')
    7 
    8 
    9 class YourBaseView(TemplateView):
    10     def get(self):
    11         print('generate token()')
    12         return super().get()
    13 
    14 
    15 class YourView(YourBaseView):
    16     def post(self):
    17         # if not dosomething(): return bad request
    18         return super().get()
    19 
    20 
    21 YourView().post()
    22 }}}
    23 
    24 This will print both 'generate token()' and 'render to response()'. If I only want to print 'render to response()', then this works:
    25 
    26 {{{
    27 class YourView(YourBaseView):
    28     def post(self):
    29         # if not dosomething(): return bad request
    30         return TemplateView.get(self)
    31 }}}
    32 
    33 But probably it would make sense to just move the render to response outside get() as such:
    34 
    35 {{{
    36 class TemplateView(object):
    37     def render_to_response(self):
    38         print('render to response()')
    39 
    40     def get(self):
    41         return self.render_to_response()
    42 
    43 
    44 class YourBaseView(TemplateView):
    45     def get(self):
    46         print('generate token()')
    47         return super().get()
    48 
    49 
    50 class YourView(YourBaseView):
    51     def post(self):
    52         # if not dosomething(): return bad request
    53         return self.render_to_response()
    54 }}}
    55 
    56 I thought it would be better that TemplateView add a new method to render a response with a context, and use that in its get(), so TemplateView logic could be more reusable.
     1Yes, returning super().get() allows to bypass the last level of get() override and i think this had a side effect in some code i'm trying to remember.
Back to Top