Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我目前正在尝试使用 Django 1.5 进入“基于类的视图”。
从设计的角度来看,我想知道将处理来自简单 FormView 表单的数据的逻辑放在哪里。
我知道所有表单验证代码都包含在方法form_valid()中。但是将处理表单数据的东西放在哪里。我读到在form_valid()方法中加入太多逻辑有点不合适。
form_valid()
有get(), post(), get_context_data(), head(), 等方法......在这种情况下我应该使用哪个?
get()
post()
get_context_data()
head()
这个问题的任何答案都可以讨论。也就是说,视图只是 Python 类,因此您可以覆盖任何方法来相应地自定义事物。
在您的类上创建一个额外的方法来处理数据处理也是完全合法的。
表单验证、数据清理等与 clean 方法中的表单类一起使用
有效表单的处理应采用被覆盖的form_valid方法
form_valid
就是这样!如果您的用例更复杂,您可以从form_valid...调用其他创建方法