有关FormTools内部的问题(Questions about the Internal of FormTools)

要使用FormPreview,我应该将其子类化并将其用作View(?)。 但FormPreview不是一个视图。 这是如何工作的?

FormPreview有很多方法。 他们在哪里打电话? 例如post_post 。 它从来没有在FormPreview类中被调用过,它没有被django调用。 或者至少我的grep -inRI post_post没有找到它的用法。 这是如何运作的?

如果我在process_preview(子类和重写方法)中设置了像self.number = 42这样的属性,那么我可以在done -method中访问它。 所以我想我正在研究同一个对象。 但我不知道对象生命周期是如何工作的。 有人可以解释吗?

To use FormPreview I should subclass it and use it as View(?). But FormPreview is not a view. How does this works?

FormPreview has a lot of methods. Where are they called? For example post_post. It is never called in the FormPreview Class it is not called in not called by django. Or at least my grep -inRI post_post did not find its usage. How does that work?

If I set some attribute like self.number = 42 in process_preview (subclassed and overridden method), then I can access this in the done-method. So I guess I am working on the same object. But I don't know how the Object life cycle works. Can someone explain?

最满意答案

一个视图可以是任何可调用的 - 并且不需要成为一个类,就像在基于函数的视图中一样。 在这种情况下,可调用对象是FormPreview类的一个实例:它是可调用的,因为该类定义了__call__方法。

post_post方法 - 以及其他方法(如preview_get )在第30行中调用,它根据阶段和请求方法动态查找要调用的方法。

A view can be any callable - and doesn't need to be a class at all, as in function-based views. In this case, the callable object is an instance of the FormPreview class: it is callable because the class defines a __call__ method.

The post_post method - along with other methods like preview_get - is called in line 30, which dynamically looks up which method to call based on the stage and the request method.

更多推荐