Created
June 6, 2011 05:49
-
-
Save sontek/1009793 to your computer and use it in GitHub Desktop.
Proper way to use django class based views
This file contains 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.generic.base import TemplateResponseMixin | |
from django.views.generic.base import View | |
class SomeFormView(TemplateResponseMixin, View): | |
template_name = 'some_form.html' | |
def get(self, request): | |
form = SomeForm() | |
return self.render_to_response({ | |
'form': form, | |
}) | |
def post(self, request): | |
form = SomeForm(request.POST) | |
if form.is_valid(): | |
form.save() | |
messages.success(request, 'Your form has been saved!') | |
return self.render_to_response({ | |
'form': form, | |
}) | |
class AjaxThingView(View): | |
# Note that I don't subclass the TemplateResponseMixin here! | |
def get(self, request): | |
return HttpResponse(status=404) | |
def post(self, request): | |
id = request.POST.get('id') | |
# Do something with the id | |
return HttpResponse('some data') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment