Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you have is a python error -- <code>self</code> is not defined. <code>self</code> is generally what refers to the class instance itself on class methods. </p> <p>Anyways, I agree, it's brand spanking new and not as documented. I think looking at the source is absolutely key at this point.</p> <p>To get comfortable with class based views, I'd start by subclassing <code>django.views.generic.base.View</code>, which implements only a few methods, namely attempting to call a function on the class based on the request method (post, get, head, - look at source). </p> <p>For example, here's the first step to replace view functions with the new view classes:</p> <pre><code>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()) </code></pre> <p><strong>Back to your specific question:</strong></p> <p>All <code>TemplateView.as_view()</code> does is render the template - <code>CreateView</code> is a combination of several other classes that handle <code>ModelForms</code> and template rendering (<code>TemplateView</code>). </p> <p>So, for a very basic example, <a href="https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-editing/#createview" rel="noreferrer">look to the docs</a> for what class <code>mixins</code> are used by <code>CreateView</code>. </p> <p>We see it implements <code>TemplateResponseMixin</code>, <code>ModelFormMixin</code>, and <code>ProcessFormView</code>, each containing a list of methods for those classes.</p> <hr> <h2>The most basic CreateView</h2> <p>At the most basic level, provide <code>CreateView</code>'s <code>ModelFormMixin</code> with the model or custom ModelForm class <a href="https://docs.djangoproject.com/en/1.8/ref/class-based-views/mixins-editing/#modelformmixin" rel="noreferrer">as documented here.</a></p> <p>Your <code>CreateView</code> class would look something like the following</p> <pre><code>class AuthorCreateView(CreateView): form_class = AuthorForm template_name = 'author_new.html' success_url = 'success' </code></pre> <p>With those 3 core attributes set, call it in your URLs.</p> <pre><code>('^authors/create/$', Author.AuthorCreateView.as_view()), </code></pre> <p>Render the page and you'll see your ModelForm passed to the template as <code>form</code>, handling the form validation step (passing in <code>request.POST</code> / re-render if invalid), as well as calling <code>form.save()</code> and redirecting to the <code>success_url</code>.</p> <hr> <h2>Start overriding the class methods</h2> <p>To customize behavior, start overriding the methods documented for the <code>mixins</code>.</p> <p>Remember that you simply need to return an <code>HttpResponse</code> from one of these methods just like any regular view function.</p> <p>Example overriding <code>form_invalid</code> documented in <code>ModelFormMixin</code>:</p> <pre><code>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") </code></pre> <hr> <p>This per-method overriding starts becoming extremely useful as your forms grow more advanced and ultimately lets you build huge forms with a handful of lines of code, overriding only what is necessary.</p> <p>Say you want to pass your form custom parameters such as the <code>request</code> object (very common if you need access to the user in the form): you merely need to override <code>get_form_kwargs</code>.</p> <pre><code>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 </code></pre> <p>Class based views are a shining example of smart class usage. It gave me a great intro towards building my own mixins for views and python classes in general. It is saving countless hours. </p> <p>Wow this got long. To think it started as a mere URL to the docs comment :) Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload