[Django 1.3] django.views.generic.TemplateView example

534 views
Skip to first unread message

Micky Hulse

unread,
Sep 30, 2011, 1:29:09 PM9/30/11
to django...@googlegroups.com
Code:

<https://gist.github.com/1254423>

When using the new class-based generic views, is there a faster way
for me to pass URL params to the template?

Thanks!
Micky

Russell Keith-Magee

unread,
Oct 1, 2011, 12:08:01 AM10/1/11
to django...@googlegroups.com
There's no need to subclass TemplateView and provide your own implementation of get_context_data() - the default TemplateView implementation does exactly (and I mean character-for-character exactly) what you've defined. So, you just need to deploy a default TemplateView in your urls.py:

('^foo/(?P<id>\d+)/$', TemplateView.as_view(template_name='my_template.html'))

Your template should work exactly as-is.

I know the class-based views docs aren't as good as they could be - thats mostly my fault. I also know that "read the source" isn't a real documentation answer. However, the CBV code is quite well documented internally, and isn't all that complex at the end of the day - if you've got questions about how to do something obscure with CBVs, a quick peruse of the source is well worth your time.

Yours,
Russ Magee %-)

Micky Hulse

unread,
Oct 5, 2011, 6:40:00 PM10/5/11
to django...@googlegroups.com
Ack! Russ, I missed this e-mail! I am so sorry for the late reply!

Thanks so much for helping me out, I really appreciate it. :)

On Fri, Sep 30, 2011 at 9:08 PM, Russell Keith-Magee
<rus...@keith-magee.com> wrote:
> There's no need to subclass TemplateView and provide your own implementation
> of get_context_data() - the default TemplateView implementation does exactly
> (and I mean character-for-character exactly) what you've defined. So, you
> just need to deploy a default TemplateView in your urls.py:

Hehe! Thanks so much for pointing that out! I should have visited the
source code before posting my question. :(

It's funny, I was just going to reply to the list with my latest
solution... Long story short, my co-worker shared this code:

urlpatterns = patterns('',
(r'^phone-list/$', ListView.as_view(
queryset=Staffer.objects.filter(active=True).order_by('last_name'),
)),
)

... and that motivated me to read the source code [1] and write this
in my urls.py:

from django.views import generic
...
(r'^(?P<id>[a-zA-Z0-9]{32})/$', generic.TemplateView.as_view(
template_name='wire/feed.html',
)),
...

Exactly what you suggested that I do!

That's great though. Thank you for taking the time to help me out, I
really appreciate it. :)

> I know the class-based views docs aren't as good as they could be - thats
> mostly my fault. I also know that "read the source" isn't a real
> documentation answer. However, the CBV code is quite well documented
> internally, and isn't all that complex at the end of the day - if you've got
> questions about how to do something obscure with CBVs, a quick peruse of the
> source is well worth your time.

That works for me! Like I said above, I should have read the source
code before bugging the group. It's amazing how clear things can be
when reading the Django source code.

From now on, I will be sure to go to the code before asking questions. :D

Thanks again! Have an awesome day.

[1] https://code.djangoproject.com/browser/django/trunk/django/views/generic/base.py#L114

Reply all
Reply to author
Forward
0 new messages