Sunday, March 27, 2011

direct_to_template generic view class-based replacement for django 1.3

I was in love with direct_to_template shortcut from django.views.generic.simple

Update: It was my lack of attention to the docs. new render shortcut do the same thing as code below

It's extremely useful to use direct_to_template instead of render_to_response because there is no necessary to put RequestContext to direct_to_template shourcut. But now it's deprecated so I decided to replace it with small class-based view which do same thing as old direct_to_template but – without restriction of new TemplateView where is no way to put extra context.

from django.views.generic.base import TemplateResponseMixin
from django.template import RequestContext


class RenderTemplate(TemplateResponseMixin):
    """django 1.3+ Class-Based replacement for
       django.views.generic.simple.direct_to_template
       Usage:
        context = {}
        return RenderTemplate.view(context, \
            request=request, template_name="main.html")
    """
    @classmethod
    def view(cls, context, render_args={}, **initkwargs):
        obj = cls()
        for key, val in initkwargs.iteritems():
            setattr(obj, key, val)

        return obj.render_to_response(context, **render_args)

    def render_to_response(self, context, **kwargs):
        return super(RenderTemplate, self)\
                    .render_to_response(RequestContext(self.request, \
                        context), **kwargs)

7 comments:

  1. The new 'render' shortcut does the same thing as far as I can tell.

    ReplyDelete
  2. Mirrorball@, thanks, you're right. I'll update the post

    ReplyDelete
  3. All the generic shortcuts have been replaced with class based views. There is the TemplateView that you can use instead. No need to subclass to create your own view.

    from django.views.generic import TemplateView
    url(r'^$', TemplateView.as_view(template_name='site/index.html') , name='index'),

    ReplyDelete
  4. Josh@, how you can put extra_context to TemplateView?

    ReplyDelete
  5. **** I don't know how ident code here sorry :(

    Max K. and Josh, it is easy extending TemplateView and reimplementing TemplateView get_context_data method :

    def get_context_data(self, **kwargs):
    context = TemplateView.get_context_data(self, **kwargs)
    return RequestContext(self.request, \
    context))

    actual definition of that method is:

    def get_context_data(self, **kwargs):
    return {
    'params': kwargs
    }

    I hope this helps.

    ReplyDelete
  6. Maybe this is better way to do it, taken in mind how TemplateResponse works:

    class MyListView(ListView):
    def render_to_response(self, context, **kwargs):
    return super(MyListView, self).render_to_response(
    RequestContext(self.request,
    context,
    processors=[custom_processor]),
    **kwargs)

    read: http://docs.djangoproject.com/en/dev/topics/generic-views-migration/

    ReplyDelete
  7. Carlitux@, new shortcut "render" doing exactly same way as good old direct_to_template. So both of your solutions above are overhead, likewise my own

    ReplyDelete