#28943: Unenforce manual get_context_data()
-------------------------------------+-------------------------------------
     Reporter:  James Pic            |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Generic views        |                  Version:  2.0
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by James Pic:

Old description:

> Currently, TemplateView inherits render_to_response(context) from
> TemplateResponseMixin which requires a context argument.
>
> This means that when your TemplateView subclass wants to return the
> TemplateResponse with the default context, you still have to create and
> pass the default context:
>
> {{{
>
> class YourView(TemplateView):
>     def post(self, request, *a, **k):
>         if not self.dostuff():
>             return http.HttpResponseBadRequest()
>
>         context = self.get_context_data()
>         return self.render_to_response(context)
>
> }}}
>
> The reason for this is that ContentMixin defines get_context_data(), and
> TemplateResponseMixin defines render_to_response(context, ...),
> TemplateResponse mixes the two in get():
>
> {{{
> class TemplateView(TemplateResponseMixin, ContextMixin, View):
>     """
>     Render a template. Pass keyword arguments from the URLconf to the
> context.
>     """
>     def get(self, request, *args, **kwargs):
>         context = self.get_context_data(**kwargs)
>         return self.render_to_response(context)
> }}}
>
> I think it would be more usable as such:
>

> {{{
> class TemplateView(TemplateResponseMixin, ContextMixin, View):
>     """
>     Render a template. Pass keyword arguments from the URLconf to the
> context.
>     """
>     def get(self, request, *args, **kwargs):
>         return self.render_to_response()
>
>     def render_to_response(self, context=None):
>         context = context or self.get_context_data(**kwargs)
>         return self.render_to_response(context)
> }}}
>
> Then, users could call render_to_response() in their code, ie:
>
> {{{
>
> class YourView(TemplateView):
>     def get(self, request, *a, **k):
>         self.token = self.generatetoken()
>         return self.render_to_response()
>
>     def post(self, request, *a, **k):
>         if not self.dostuff():
>             return http.HttpResponseBadRequest()
>         return self.render_to_response()
> }}}

New description:

 Currently, TemplateView inherits render_to_response(context) from
 TemplateResponseMixin which requires a context argument.

 This means that when your TemplateView subclass wants to return the
 TemplateResponse with the default context, you still have to create and
 pass the default context:

 {{{

 class YourView(TemplateView):
     def post(self, request, *a, **k):
         if not self.dostuff():
             return http.HttpResponseBadRequest()

         context = self.get_context_data()
         return self.render_to_response(context)

 }}}

 The reason for this is that ContentMixin defines get_context_data(), and
 TemplateResponseMixin defines render_to_response(context, ...),
 TemplateResponse mixes the two in get():

 {{{
 class TemplateView(TemplateResponseMixin, ContextMixin, View):
     """
     Render a template. Pass keyword arguments from the URLconf to the
 context.
     """
     def get(self, request, *args, **kwargs):
         context = self.get_context_data(**kwargs)
         return self.render_to_response(context)
 }}}

 I think it would be more usable as such:


 {{{
 class TemplateView(TemplateResponseMixin, ContextMixin, View):
     """
     Render a template. Pass keyword arguments from the URLconf to the
 context.
     """
     def get(self, request, *args, **kwargs):
         return self.render_to_response()

     def render_to_response(self, context=None):
         context = context or self.get_context_data(self.kwargs)
         return self.render_to_response(context)
 }}}

 Then, users could call render_to_response() in their code, ie:

 {{{

 class YourView(TemplateView):
     def get(self, request, *a, **k):
         self.token = self.generatetoken()
         return self.render_to_response()

     def post(self, request, *a, **k):
         if not self.dostuff():
             return http.HttpResponseBadRequest()
         return self.render_to_response()
 }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28943#comment:7>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.155f6d7a6143c1b7f8029319370bffb0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to