简体   繁体   中英

Design of CBVs in Django

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?

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.

  1. Form validation, data cleaning, etc goes with the form class in the clean methods

  2. 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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM