how do django forms work

I am learning Django. Particularly I am reading about forms and I cannot understand how do they work. Lets look at example code:

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)

why create class with unbounded members? there is no self statement.

I looked on Django source code itself:

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

Could you explain what is the role of __metaclass__ ?

Answers


The form fields are descriptors, which are simply a class written to a specific protocol. The metaclass is the type of the class (since classes themselves, being objects, are instantiated from something).


Need Your Help

How to write mvc applications that are “hookable”

c# asp.net-mvc design-patterns hook

I am trying to figure out a design pattern that would allow "hooking" into an existing mvc application without re-compiling that entire application.

how to search in Mongodb by querying multiple fields in the same document

php mongodb symfony2 doctrine2 nosql

I'm going over a project I've taken over which is crashing in this doctrine/mongoDB query:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.