Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4df7e84

Browse files
pwmarcztimgraham
authored andcommitted
Fixed #24788 -- Allowed Forms to specify a prefix at the class level.
1 parent 4ccfc44 commit 4df7e84

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

django/forms/forms.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class BaseForm(object):
7575
# information. Any improvements to the form API should be made to *this*
7676
# class, not to the Form class.
7777
field_order = None
78+
prefix = None
7879

7980
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
8081
initial=None, error_class=ErrorList, label_suffix=None,
@@ -83,7 +84,8 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
8384
self.data = data or {}
8485
self.files = files or {}
8586
self.auto_id = auto_id
86-
self.prefix = prefix
87+
if prefix is not None:
88+
self.prefix = prefix
8789
self.initial = initial or {}
8890
self.error_class = error_class
8991
# Translators: This is the default suffix added to form field labels

docs/ref/forms/api.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,3 +1065,13 @@ You can put several Django forms inside one ``<form>`` tag. To give each
10651065
>>> print(father.as_ul())
10661066
<li><label for="id_father-first_name">First name:</label> <input type="text" name="father-first_name" id="id_father-first_name" /></li>
10671067
<li><label for="id_father-last_name">Last name:</label> <input type="text" name="father-last_name" id="id_father-last_name" /></li>
1068+
1069+
The prefix can also be specified on the form class::
1070+
1071+
>>> class PersonForm(forms.Form):
1072+
... ...
1073+
... prefix = 'person'
1074+
1075+
.. versionadded:: 1.9
1076+
1077+
The ability to specify ``prefix`` on the form class was added.

docs/releases/1.9.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ Forms
161161
:attr:`~django.forms.Form.field_order` attribute, the ``field_order``
162162
constructor argument , or the :meth:`~django.forms.Form.order_fields` method.
163163

164+
* A form prefix can be specified inside a form class, not only when
165+
instantiating a form. See :ref:`form-prefix` for details.
166+
164167
Generic Views
165168
^^^^^^^^^^^^^
166169

tests/forms_tests/tests/test_forms.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,18 @@ def add_prefix(self, field_name):
16711671
self.assertEqual(p.cleaned_data['last_name'], 'Lennon')
16721672
self.assertEqual(p.cleaned_data['birthday'], datetime.date(1940, 10, 9))
16731673

1674+
def test_class_prefix(self):
1675+
# Prefix can be also specified at the class level.
1676+
class Person(Form):
1677+
first_name = CharField()
1678+
prefix = 'foo'
1679+
1680+
p = Person()
1681+
self.assertEqual(p.prefix, 'foo')
1682+
1683+
p = Person(prefix='bar')
1684+
self.assertEqual(p.prefix, 'bar')
1685+
16741686
def test_forms_with_null_boolean(self):
16751687
# NullBooleanField is a bit of a special case because its presentation (widget)
16761688
# is different than its data. This is handled transparently, though.

0 commit comments

Comments
 (0)