Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created September 30, 2011 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhulse/1254423 to your computer and use it in GitHub Desktop.
Save mhulse/1254423 to your computer and use it in GitHub Desktop.
[Django 1.3] django.views.generic.TemplateView example
"{{ params.id }}"
from django.conf.urls.defaults import *
from django.views import generic
# from <app_name> import views
urlpatterns = patterns('',
# (r'^(?P<id>[a-zA-Z0-9]{32})/$', views.Show.as_view()),
# Example: d0732c86f9b44a428fc30e935ef90fcf
(r'^(?P<id>[a-zA-Z0-9]{32})/$', generic.TemplateView.as_view(
template_name='<app_name>/feed.html',
)),
)
from django.views import generic
"""
class Show(generic.TemplateView):
template_name = 'wire/feed.html'
def get_context_data(self, **kwargs):
return {
'params': kwargs,
}
"""
@mhulse
Copy link
Author

mhulse commented Sep 30, 2011

Is there a faster way of doing the above?

Discussion found here.

@eppsilon
Copy link

You could use a plain (not class-based) view function and the render() or render_to_response() shortcut. But that would require only marginally less code.

@mhulse
Copy link
Author

mhulse commented Sep 30, 2011

Thanks for the help eppsilon! I really appreciate it. :)

I definitely don't mind the code above... Basically I just wanted to make sure that I was properly using the "TemplateView", class-based generic view, for something as simple as passing URL param to a template.

I feel pretty comfortable with the old functional view stuff, but for some reason the new class-based views have been giving me trouble. :D

Thanks so much for you help and taking the time to give a helpful answer.

Cheers,
Micky

@mhulse
Copy link
Author

mhulse commented Oct 5, 2011

I posted an update in my urls.py file; I suppose that is one way to skip a view and go directly to template.

Looking at TemplateView class in the Django source code, kwargs is already passed to the template as a variable labeled "params".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment