Ticket #3132: newforms-prefix.diff
File newforms-prefix.diff, 5.4 KB (added by , 18 years ago) |
---|
-
django/newforms/forms.py
36 36 "A collection of Fields, plus their associated data." 37 37 __metaclass__ = DeclarativeFieldsMetaclass 38 38 39 def __init__(self, data=None, auto_id='id_%s' ): # TODO: prefix stuff39 def __init__(self, data=None, auto_id='id_%s', prefix=None): 40 40 self.ignore_errors = data is None 41 41 self.data = data or {} 42 42 self.auto_id = auto_id 43 self.prefix = prefix 43 44 self.clean_data = None # Stores the data after clean() has been called. 44 45 self.__errors = None # Stores the errors after clean() has been called. 45 46 … … 48 49 49 50 def __iter__(self): 50 51 for name, field in self.fields.items(): 52 # TODO: This prefix code probably shouldn't be in so many places. 53 if self.prefix: 54 name = "%s.%s" % (self.prefix, name) 51 55 yield BoundField(self, field, name) 52 56 53 57 def __getitem__(self, name): … … 56 60 field = self.fields[name] 57 61 except KeyError: 58 62 raise KeyError('Key %r not found in Form' % name) 63 # TODO: This prefix code probably shouldn't be in so many places. 64 if self.prefix: 65 name = "%s.%s" % (self.prefix, name) 59 66 return BoundField(self, field, name) 60 67 61 68 def _errors(self): … … 77 84 top_errors = self.non_field_errors() # Errors that should be displayed above all fields. 78 85 output, hidden_fields = [], [] 79 86 for name, field in self.fields.items(): 87 # TODO: This prefix code probably shouldn't be in so many places. 88 if self.prefix: 89 name = "%s.%s" % (self.prefix, name) 80 90 bf = BoundField(self, field, name) 81 91 bf_errors = bf.errors # Cache in local variable. 82 92 if bf.is_hidden: … … 129 139 self.__errors = errors 130 140 return 131 141 for name, field in self.fields.items(): 142 # TODO: This prefix code probably shouldn't be in so many places. 143 if self.prefix: 144 prefixed_name = "%s.%s" % (self.prefix, name) 145 else: 146 prefixed_name = name 132 147 # value_from_datadict() gets the data from the dictionary. 133 148 # Each widget type knows how to retrieve its own data, because some 134 149 # widgets split data over several HTML fields. 135 value = field.widget.value_from_datadict(self.data, name)150 value = field.widget.value_from_datadict(self.data, prefixed_name) 136 151 try: 137 152 value = field.clean(value) 138 153 self.clean_data[name] = value -
tests/regressiontests/forms/tests.py
1924 1924 <li>Password1: <input type="password" name="password1" /></li> 1925 1925 <li>Password (again): <input type="password" name="password2" /></li> 1926 1926 1927 # Forms with prefixes ######################################################### 1928 1929 Sometimes you'd like to have multiple forms display on the same html page, or 1930 maybe just multiple copies of the same form. We can accomplish this with form 1931 prefixes. 1932 1933 >>> class Person(Form): 1934 ... first_name = CharField() 1935 ... last_name = CharField() 1936 ... birthday = DateField() 1937 1938 Pass a dictionary to a Form's __init__(), but also pass in the keyword argument 1939 'prefix'. This value will be prepended to each html form field name. One way to 1940 think about this is "namespaces for html forms". Notice that in the data 1941 argument, each field's key has the prefix, in this case 'person1', prepended 1942 to the actual field name. 1943 >>> data = { 1944 ... 'person1.first_name': u'John', 1945 ... 'person1.last_name': u'Lennon', 1946 ... 'person1.birthday': u'1940-10-9' 1947 ... } 1948 >>> p = Person(data, prefix='person1') 1949 >>> print p['first_name'] 1950 <input type="text" name="person1.first_name" value="John" id="id_person1.first_name" /> 1951 >>> print p['last_name'] 1952 <input type="text" name="person1.last_name" value="Lennon" id="id_person1.last_name" /> 1953 >>> print p['birthday'] 1954 <input type="text" name="person1.birthday" value="1940-10-9" id="id_person1.birthday" /> 1955 >>> p.errors 1956 {} 1957 >>> p.is_valid() 1958 True 1959 1960 This is pretty unremarkable in and of itself, but let's create some data that 1961 contains info for two different people. 1962 >>> data = { 1963 ... 'person1.first_name': u'John', 1964 ... 'person1.last_name': u'Lennon', 1965 ... 'person1.birthday': u'1940-10-9', 1966 ... 'person2.first_name': u'Jim', 1967 ... 'person2.last_name': u'Morrison', 1968 ... 'person2.birthday': u'1943-12-8' 1969 ... } 1970 1971 If we use the correct prefix argument, we can create two different forms that 1972 will only use and validate the data for fields with a matching prefix. 1973 >>> p1 = Person(data, prefix='person1') 1974 >>> p1.is_valid() 1975 True 1976 >>> p1.clean_data 1977 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 1978 1979 >>> p2 = Person(data, prefix='person2') 1980 >>> p2.is_valid() 1981 True 1982 >>> p2.clean_data 1983 {'first_name': u'Jim', 'last_name': u'Morrison', 'birthday': datetime.date(1943, 12, 8)} 1984 1927 1985 # Basic form processing in a view ############################################# 1928 1986 1929 1987 >>> from django.template import Template, Context