Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

newforms: Added BoundField.as_hidden()

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4145 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4dca65cdfc1e206fac323bd8afae9ae1fccc9dac 1 parent d506b90
@adrianholovaty adrianholovaty authored
Showing with 15 additions and 3 deletions.
  1. +7 −1 django/newforms/forms.py
  2. +8 −2 tests/regressiontests/forms/tests.py
View
8 django/newforms/forms.py
@@ -5,7 +5,7 @@
from django.utils.datastructures import SortedDict
from django.utils.html import escape
from fields import Field
-from widgets import TextInput, Textarea
+from widgets import TextInput, Textarea, HiddenInput
from util import ErrorDict, ErrorList, ValidationError
NON_FIELD_ERRORS = '__all__'
@@ -194,6 +194,12 @@ def as_textarea(self, attrs=None):
"Returns a string of HTML for representing this as a <textarea>."
return self.as_widget(Textarea(), attrs)
+ def as_hidden(self, attrs=None):
+ """
+ Returns a string of HTML for representing this as an <input type="hidden">.
+ """
+ return self.as_widget(HiddenInput(), attrs)
+
def _data(self):
"Returns the data for this BoundField, or None if it wasn't given."
return self._form.data.get(self._name, None)
View
10 tests/regressiontests/forms/tests.py
@@ -1436,11 +1436,14 @@
>>> print f['message']
<textarea name="message"></textarea>
-as_textarea() and as_text() are shortcuts for changing the output widget type:
+as_textarea(), as_text() and as_hidden() are shortcuts for changing the output
+widget type:
>>> f['subject'].as_textarea()
u'<textarea name="subject"></textarea>'
>>> f['message'].as_text()
u'<input type="text" name="message" />'
+>>> f['message'].as_hidden()
+u'<input type="hidden" name="message" />'
The 'widget' parameter to a Field can also be an instance:
>>> class ContactForm(Form):
@@ -1450,7 +1453,8 @@
>>> print f['message']
<textarea rows="80" cols="20" name="message"></textarea>
-Instance-level attrs are *not* carried over to as_textarea() and as_text():
+Instance-level attrs are *not* carried over to as_textarea(), as_text() and
+as_hidden():
>>> f['message'].as_text()
u'<input type="text" name="message" />'
>>> f = ContactForm({'subject': 'Hello', 'message': 'I love you.'})
@@ -1458,6 +1462,8 @@
u'<textarea name="subject">Hello</textarea>'
>>> f['message'].as_text()
u'<input type="text" name="message" value="I love you." />'
+>>> f['message'].as_hidden()
+u'<input type="hidden" name="message" value="I love you." />'
For a form with a <select>, use ChoiceField:
>>> class FrameworkForm(Form):
Please sign in to comment.
Something went wrong with that request. Please try again.