| | 1927 | # Forms with prefixes ######################################################### |
|---|
| | 1928 | |
|---|
| | 1929 | Sometimes it's necessary to have multiple forms display on the same HTML page, |
|---|
| | 1930 | or multiple copies of the same form. We can accomplish this with form prefixes. |
|---|
| | 1931 | Pass the keyword argument 'prefix' to the Form constructor to use this feature. |
|---|
| | 1932 | This value will be prepended to each HTML form field name. One way to think |
|---|
| | 1933 | about this is "namespaces for HTML forms". Notice that in the data argument, |
|---|
| | 1934 | each field's key has the prefix, in this case 'person1', prepended to the |
|---|
| | 1935 | actual field name. |
|---|
| | 1936 | >>> class Person(Form): |
|---|
| | 1937 | ... first_name = CharField() |
|---|
| | 1938 | ... last_name = CharField() |
|---|
| | 1939 | ... birthday = DateField() |
|---|
| | 1940 | >>> data = { |
|---|
| | 1941 | ... 'person1-first_name': u'John', |
|---|
| | 1942 | ... 'person1-last_name': u'Lennon', |
|---|
| | 1943 | ... 'person1-birthday': u'1940-10-9' |
|---|
| | 1944 | ... } |
|---|
| | 1945 | >>> p = Person(data, prefix='person1') |
|---|
| | 1946 | >>> print p.as_ul() |
|---|
| | 1947 | <li><label for="id_person1-first_name">First name:</label> <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /></li> |
|---|
| | 1948 | <li><label for="id_person1-last_name">Last name:</label> <input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" /></li> |
|---|
| | 1949 | <li><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" /></li> |
|---|
| | 1950 | >>> print p['first_name'] |
|---|
| | 1951 | <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /> |
|---|
| | 1952 | >>> print p['last_name'] |
|---|
| | 1953 | <input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" /> |
|---|
| | 1954 | >>> print p['birthday'] |
|---|
| | 1955 | <input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" /> |
|---|
| | 1956 | >>> p.errors |
|---|
| | 1957 | {} |
|---|
| | 1958 | >>> p.is_valid() |
|---|
| | 1959 | True |
|---|
| | 1960 | |
|---|
| | 1961 | This is pretty unremarkable in and of itself, but let's create some data that |
|---|
| | 1962 | contains info for two different people. |
|---|
| | 1963 | >>> data = { |
|---|
| | 1964 | ... 'person1-first_name': u'John', |
|---|
| | 1965 | ... 'person1-last_name': u'Lennon', |
|---|
| | 1966 | ... 'person1-birthday': u'1940-10-9', |
|---|
| | 1967 | ... 'person2-first_name': u'Jim', |
|---|
| | 1968 | ... 'person2-last_name': u'Morrison', |
|---|
| | 1969 | ... 'person2-birthday': u'1943-12-8' |
|---|
| | 1970 | ... } |
|---|
| | 1971 | |
|---|
| | 1972 | If we use the correct prefix argument, we can create two different forms that |
|---|
| | 1973 | will only use and validate the data for fields with a matching prefix. |
|---|
| | 1974 | >>> p1 = Person(data, prefix='person1') |
|---|
| | 1975 | >>> p1.is_valid() |
|---|
| | 1976 | True |
|---|
| | 1977 | >>> p1.clean_data |
|---|
| | 1978 | {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} |
|---|
| | 1979 | |
|---|
| | 1980 | >>> p2 = Person(data, prefix='person2') |
|---|
| | 1981 | >>> p2.is_valid() |
|---|
| | 1982 | True |
|---|
| | 1983 | >>> p2.clean_data |
|---|
| | 1984 | {'first_name': u'Jim', 'last_name': u'Morrison', 'birthday': datetime.date(1943, 12, 8)} |
|---|
| | 1985 | |
|---|
| | 1986 | By default, forms append a hyphen between the prefix and the field name, but a |
|---|
| | 1987 | form can alter that behavior by implementing the add_prefix() method. This |
|---|
| | 1988 | method takes a field name and returns the prefixed field, according to |
|---|
| | 1989 | self.prefix. |
|---|
| | 1990 | >>> class Person(Form): |
|---|
| | 1991 | ... first_name = CharField() |
|---|
| | 1992 | ... last_name = CharField() |
|---|
| | 1993 | ... birthday = DateField() |
|---|
| | 1994 | ... def add_prefix(self, field_name): |
|---|
| | 1995 | ... return self.prefix and '%s-prefix-%s' % (self.prefix, field_name) or field_name |
|---|
| | 1996 | >>> p = Person(prefix='foo') |
|---|
| | 1997 | >>> print p.as_ul() |
|---|
| | 1998 | <li><label for="id_foo-prefix-first_name">First name:</label> <input type="text" name="foo-prefix-first_name" id="id_foo-prefix-first_name" /></li> |
|---|
| | 1999 | <li><label for="id_foo-prefix-last_name">Last name:</label> <input type="text" name="foo-prefix-last_name" id="id_foo-prefix-last_name" /></li> |
|---|
| | 2000 | <li><label for="id_foo-prefix-birthday">Birthday:</label> <input type="text" name="foo-prefix-birthday" id="id_foo-prefix-birthday" /></li> |
|---|
| | 2001 | >>> data = { |
|---|
| | 2002 | ... 'foo-prefix-first_name': u'John', |
|---|
| | 2003 | ... 'foo-prefix-last_name': u'Lennon', |
|---|
| | 2004 | ... 'foo-prefix-birthday': u'1940-10-9' |
|---|
| | 2005 | ... } |
|---|
| | 2006 | >>> p = Person(data, prefix='foo') |
|---|
| | 2007 | >>> p.is_valid() |
|---|
| | 2008 | True |
|---|
| | 2009 | >>> p.clean_data |
|---|
| | 2010 | {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} |
|---|
| | 2011 | |
|---|