1
我正在学习Django。特别是我正在阅读有关forms,我不明白他们是如何工作的。 让我们看看示例代码:django表单如何工作
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
为什么无界成员建立的类?没有self
声明。
我看着Django的源代码本身:
class Form(BaseForm):
"A collection of Fields, plus their associated data."
# This is a separate class from BaseForm in order to abstract the way
# self.fields is specified. This class (Form) is the one that does the
# fancy metaclass stuff purely for the semantic sugar -- it allows one
# to define a form using declarative syntax.
# BaseForm itself has no way of designating self.fields.
__metaclass__ = DeclarativeFieldsMetaclass
你能不能解释一下什么是__metaclass__
作用?
这里有一些东西可以阅读:http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python – Blender
'ContactForm'类没有未绑定的成员;它只有属性。这在Python中是完全合法的。 –