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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.