You have a python error - self not defined. self usually refers to the class instance itself in class methods.
In any case, I agree, this brand spanks a new one and is not documented. I think that looking at the source is absolutely key at this point.
To get comfortable with class-based views, I would start by subclassing django.views.generic.base.View , which implements only a few methods, namely an attempt to call a class function based on the request method (post, get, head, - look at source).
For example, here is the first step to replace view functions with new view classes:
class MyClassBasedView(View): def get(self, request):
Back to your specific question:
All TemplateView.as_view() does the visualization of the template - CreateView is a combination of several other classes that handle ModelForms and template rendering ( TemplateView ).
So, for a very simple example, look at the documents for which the mixins class is used by CreateView .
We see that it implements TemplateResponseMixin , ModelFormMixin and ProcessFormView , each of which contains a list of methods for these classes.
The easiest CreateView
At the most basic level, provide the CreateView ModelFormMixin model or custom ModelForm class as described here.
Your CreateView class will look something like this:
class AuthorCreateView(CreateView): form_class = AuthorForm template_name = 'author_new.html' success_url = 'success'
Given these three basic attributes, enter them in your URLs.
('^authors/create/$', Author.AuthorCreateView.as_view()),
Display the page and you will see that your ModelForm is passed to the template as form , processing the form validation step (passing to request.POST / re-render if it is invalid), and also calls form.save() and redirects to success_url .
Start overriding class methods
To customize the behavior, start overriding the methods documented for mixins .
Remember that you just need to return the HttpResponse from one of these methods, like any regular view function.
An example override of form_invalid documented in ModelFormMixin :
class AuthorCreateView(CreateView): form_class = AuthorForm template_name = 'author_new.html' success_url = 'success' def form_invalid(self, form): return http.HttpResponse("form is invalid.. this is just an HttpResponse object")
This overriding method starts extremely useful as your forms become more advanced and ultimately allow you to create huge forms with a few lines of code, overriding only what you need.
Suppose you want to pass your custom form parameters, such as a request object (very common if you need access to the user on the form): you just need to override get_form_kwargs .
class MyFormView(FormView): def get_form_kwargs(self):
Class-based views are a prime example of the use of the intellectual class. This gave me a great introduction to creating my own mixes for views and python classes in general. It saves countless hours.
Wow it worked out. Thinking it started as a simple document comment url :) Hope this helps!