Skip to content

Instantly share code, notes, and snippets.

@sontek
Created June 6, 2011 05:49
Show Gist options
  • Save sontek/1009793 to your computer and use it in GitHub Desktop.
Save sontek/1009793 to your computer and use it in GitHub Desktop.
Proper way to use django class based views
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