[Django]-Render Django view class to either string or response

4👍

I’d follow in django’s CBV pattern: it determines via dispatch what method to return. By default based on request.method. Why not based on any other argument passed to dispatch()?

So subclass dispatch and give it a way to determine whether or not to return get_string.

def dispatch(self, request, *args, **kwargs):
    if 'as_string' in kwargs:
         return self.get_string(request)        
    return super(TemplateView, self).dispatch(request, *args, **kwargs)

response = TemplateView.as_view()(request, as_string=True)

Leave a comment