Slide 23
Slide 23 text
COMMON
METHODS
dispatch
get, post, put,
delete
get_context_data
get_object
get_queryset
get_form_class
get_form_kwargs
get_success_url
form_valid
form_invalid
23
Tuesday, September 4, 12
When working with CBVs, these methods are ones you commonly find yourself overwriting.
Dispatch handles calling the correct method based on the HTTP verb requested. These would
be get, post, put, or delete.
Get_context_data fills out the kwargs passed to your template. You can override this to add in
new keys.
Get_object handles getting the object from the database or, more likely, the queryset.
Get_queryset controls the generation of the query to the ORM.
Get_form_class and get_form_kwargs handle instantiating the correct form class with the
correct args.
Get_success_url gets the URL to use when a submitted form is successful, and is called by:
Form_valid, which handles what to do when a form is valid (save the instance, run a Celery
task, etc).
Form_invalid, lastly, handles the opposite of that, usually including re-rendering the view
with errors.