I'm currently trying to get into "Class Based Views" with Django 1.5.
From the design perspective i wonder where to put the logic to process data comming from a form in a simple FormView.
I know that all form validation code comes into the method form_valid()
. But where to put things which processes data of the form. I read that its somehow inappropriate to put too much logic into the form_valid()
method.
There are the get()
, post()
, get_context_data()
, head()
, etc. methods... which should I use in which case?
Form validation, data cleaning, etc goes with the form class in the clean methods
Processing of a valid form should go in an overridden form_valid
method
That's it! If your use-case is more complicated you can call out to other methods of your creation from form_valid
...
Any answer to this question is open for discussion. That said, views are just Python classes, so you could overwrite any method to customize things accordingly.
It is also perfectly legit to create an extra method on your class to handle data processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With