Created
September 19, 2012 14:02
-
-
Save bmispelon/3749847 to your computer and use it in GitHub Desktop.
Quick explanations of django's CBV
This file contains hidden or 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
| django.views.generic.base: | |
| View: | |
| The most basic class. All other class based views inherit from it. | |
| To use it, create a method for each HTTP verb (get, post, put, ...). | |
| These method get passed a request obejct as the first argument, as well | |
| as whatever was captured in the url (*args, or **kwargs). | |
| The request, the *args and **kwargs are stored on the instance. | |
| To instanciate a view in your code, use `view_name = ClassBasedView.as_view()`. | |
| TemplateView: | |
| A view that displays a template. | |
| The name of the template can be specified by defining the `template_name` | |
| attribute on the class. | |
| To pass data to the template's context, override the `get_context_data` method. | |
| Note that this method should be able to take an arbitrary numbers of keyword arguments. | |
| The default implementation puts the given kwargs in the context. | |
| django.views.generic.detail: | |
| DetailView: | |
| Display information about one particular object. | |
| Provide a `model` attribute on the class and the instance of that model | |
| with the id corresponding to what was captured in the URL will be | |
| put in the context of the rendered template. | |
| You can change the name of the capturing URL parameter (by default, it | |
| uses "pk") with the `pk_url_kwargs` attribute. | |
| django.views.generic.edit: | |
| FormView: | |
| Process a form. When called with GET, display the form. With POST, | |
| try to validate the form. Re-display it if it's not valid. Redirect otherwise. | |
| To use it, define the `form_class` attribute on it, as well as the | |
| `success_url` one (don't forget to user reverse_lazy). | |
| The `form_valid` and `form_invalid` method can be overriden to do things | |
| when the form is valid/invalid (using super(...) to call the parent class's | |
| behavior is a good idea here). | |
| CreateView: | |
| Create an instance of a model. | |
| To use it, define either the `model` or the `form_class` attribute. | |
| It's mostly used like a FormView, the only difference being | |
| that the new object is saved when the form is validated. | |
| UpdateView: | |
| Update an existing instance of a model. | |
| To use it, define a `model` attribute. You can also define `form_class` | |
| if you have a custom modelform. | |
| Like in DetailView, you can tweak `pk_url_kwarg` to reflect the name | |
| of the capture parameter in the URL. | |
| DeleteView: | |
| Delete an existing instance of a model. | |
| When called with GET, display a confirmation. With POST, perform the deletion. | |
| Similar to DetailView in regard to retrieving the model instance. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment