The rationale behind class-based views is that they are meant to be reused.
While all of the listed approaches work, CBVs have greater functionality and
modularity. It might seem like more code at first, but the possibility of
using Mixins and overriding just bits and pieces of other views you have
make your views easier to refactor/maintain in the long run.

And given that CBVs are very new to django, suggestions on how to improve
them and reduce boilerplate are always welcome. I'm sure there is lots of
room for improvement.


Sincerely,
Andre Terra

On Wed, Mar 23, 2011 at 7:31 AM, Kevin Renskers <i...@bolhoed.net> wrote:

> Hi,
>
> I am wondering why Django 1.3 has both class-based generic views (like
> TemplateView and RedirectView) and shortcuts like django.shortcut.render and
> django.shortcut.redirect. What is the recommended way to write your views?
> Is a call to render() from within your own view function better then using
> TemplateView, or visa versa? It certainly looks easier...
>
> As a rule I never used generic views directly from urls.py, I always
> created a view function. So it seems that for me, I could just replace
> direct_to_template with render and I'm done.
>
> Old:
>
> def home(request):
>     return direct_to_template(request, template='home.html',
> extra_context={'foo': 42,'bar': 37})
>
> New:
>
> def home(request):
>     return render(request, template='home.html', dictionary={'foo':
> 42,'bar': 37})
>
> Or the alternative:
>
> class Home(TemplateView):
>     template_name = 'home.html'
>
>     def get_context_data(self, **kwargs):
>         context = super(Home, self).get_context_data(**kwargs)
>         context.update({
>             'foo': 42,
>             'bar': 37
>         })
>         return context
>
> In the last case, I also have to change my urls config to now use
> Home.as_view(), which needs to be imported, etc. Seems like a lot of code
> and repeating myself.
>
> Thanks,
> Kevin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to