How to use CreateView with ModelForm

I get an error in my AuthorCreateForm class when submitting my form. NameError self not defined

How to use CreateForm?

I created a class in the file Author.py

from django.views.generic import TemplateView, ListView, CreateView from books.models import Author, Publisher, Book from books.forms import AuthorForm class AuthorCreateView(CreateView): objAuthorForm = AuthorForm(self.request.POST) if(objAuthorForm.save()): success = "Form saved!" else: error = "There was an error!" 

and I have an html template that sends / Author / Create

and I have the following line in my urls.py

 ('^authors/create/$', Author.AuthorCreateView.as_view()), 

I submit the form at this URL

 ('^authors/new/$', TemplateView.as_view(template_name="author_new.html")), 

I find class-based views confusing, does anyone have a good tutorial on how to use it for CRUD operations?

thank

+43
django django-views
Apr 24 '11 at 10:40
source share
1 answer

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): # behave exactly like old style views # except this is called only on get request return http.HttpResponse("Get") def post(self, request): return http.HttpResponse("Post") (r'^foobar/$', MyClassBasedView.as_view()) 

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): # pass "user" keyword argument with the current user to your form kwargs = super(MyFormView, self).get_form_kwargs() kwargs['user'] = self.request.user return kwargs 

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!

+167
Apr 25 '11 at 0:20
source share



All Articles