Created
September 30, 2011 17:26
-
-
Save mhulse/1254423 to your computer and use it in GitHub Desktop.
[Django 1.3] django.views.generic.TemplateView example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "{{ params.id }}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | |
| )), | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.views import generic | |
| """ | |
| class Show(generic.TemplateView): | |
| template_name = 'wire/feed.html' | |
| def get_context_data(self, **kwargs): | |
| return { | |
| 'params': kwargs, | |
| } | |
| """ |
Author
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
Author
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
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.