Created
January 5, 2012 00:34
-
-
Save gladson/1563059 to your computer and use it in GitHub Desktop.
Generic Views by Flavia missi
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
| # Simple json response generic view | |
| from django.views.generic import View | |
| from django.utils import simplejson | |
| class JSONResponseView(View): | |
| def render_to_response(self, data, **httpresponse_kwargs): | |
| "Retuns a json response based on the context" | |
| json_data = simplejson.dumps(data) | |
| return HttpResponse(json_data, content_type="application/json", **httpresponse_kwargs) | |
| # Django UpdateView sample | |
| #views.py | |
| from django.views.generic import UpdateView | |
| class UpdateBook(UpdateView): | |
| model = Book | |
| form_class = BookForm | |
| template_name = 'create_form.html' | |
| success_url = '/books/' | |
| #urls.py | |
| from django.conf.urls import * | |
| from library.views import UpdateBook | |
| urlpatterns = patterns('', | |
| url('^update_book/(?P<pk>[\w-]+)$', UpdateBook.as_view(), name='update_book'), | |
| ) | |
| # you MUST pass a pk or slug param to this view | |
| # Django CreateView sample | |
| from django.views.generic import CreateView | |
| from library.forms import BookForm | |
| class CreateBook(CreateView): | |
| template_name = 'create_form.html' | |
| success_url = '/books/' | |
| form_class = BookForm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment