Commit 08b501e7 authored by leandrafinger's avatar leandrafinger Committed by Silvan Spross
Browse files

add missing imports to the examples in the 'Forms'

parent 1d543949
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ How to use ``FormPreview``
   overrides the ``done()`` method::

       from django.contrib.formtools.preview import FormPreview
       from django.http import HttpResponseRedirect
       from myapp.models import SomeModel

       class SomeModelFormPreview(FormPreview):
+53 −48
Original line number Diff line number Diff line
@@ -154,6 +154,7 @@ you include ``initial`` when instantiating the ``Form``, then the latter
at the field level and at the form instance level, and the latter gets
precedence::

    >>> from django import forms
    >>> class CommentForm(forms.Form):
    ...     name = forms.CharField(initial='class')
    ...     url = forms.URLField()
@@ -238,6 +239,7 @@ When the ``Form`` is valid, ``cleaned_data`` will include a key and value for
fields. In this example, the data dictionary doesn't include a value for the
``nick_name`` field, but ``cleaned_data`` includes it, with an empty value::

    >>> from django.forms import Form
    >>> class OptionalPersonForm(Form):
    ...     first_name = CharField()
    ...     last_name = CharField()
@@ -391,6 +393,8 @@ attributes to required rows or to rows with errors: simply set the
:attr:`Form.error_css_class` and/or :attr:`Form.required_css_class`
attributes::

    from django.forms import Form

    class ContactForm(Form):
        error_css_class = 'error'
        required_css_class = 'required'
@@ -779,6 +783,7 @@ example, ``BeatleForm`` subclasses both ``PersonForm`` and ``InstrumentForm``
(in that order), and its field list includes the fields from the parent
classes::

    >>> from django.forms import Form
    >>> class PersonForm(Form):
    ...     first_name = CharField()
    ...     last_name = CharField()
+8 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ By default, each ``Field`` class assumes the value is required, so if you pass
an empty value -- either ``None`` or the empty string (``""``) -- then
``clean()`` will raise a ``ValidationError`` exception::

    >>> from django import forms
    >>> f = forms.CharField()
    >>> f.clean('foo')
    u'foo'
@@ -107,6 +108,7 @@ behavior doesn't result in an adequate label.
Here's a full example ``Form`` that implements ``label`` for two of its fields.
We've specified ``auto_id=False`` to simplify the output::

    >>> from django import forms
    >>> class CommentForm(forms.Form):
    ...     name = forms.CharField(label='Your name')
    ...     url = forms.URLField(label='Your Web site', required=False)
@@ -130,6 +132,7 @@ To specify dynamic initial data, see the :attr:`Form.initial` parameter.
The use-case for this is when you want to display an "empty" form in which a
field is initialized to a particular value. For example::

    >>> from django import forms
    >>> class CommentForm(forms.Form):
    ...     name = forms.CharField(initial='Your name')
    ...     url = forms.URLField(initial='http://')
@@ -205,6 +208,7 @@ methods (e.g., ``as_ul()``).
Here's a full example ``Form`` that implements ``help_text`` for two of its
fields. We've specified ``auto_id=False`` to simplify the output::

    >>> from django import forms
    >>> class HelpTextContactForm(forms.Form):
    ...     subject = forms.CharField(max_length=100, help_text='100 characters max.')
    ...     message = forms.CharField()
@@ -236,6 +240,7 @@ The ``error_messages`` argument lets you override the default messages that the
field will raise. Pass in a dictionary with keys matching the error messages you
want to override. For example, here is the default error message::

    >>> from django import forms
    >>> generic = forms.CharField()
    >>> generic.clean('')
    Traceback (most recent call last):
@@ -853,6 +858,7 @@ Slightly complex built-in ``Field`` classes
        The list of fields that should be used to validate the field's value (in
        the order in which they are provided).

            >>> from django.forms import ComboField
            >>> f = ComboField(fields=[CharField(max_length=20), EmailField()])
            >>> f.clean('test@example.com')
            u'test@example.com'
@@ -1001,6 +1007,8 @@ objects (in the case of ``ModelMultipleChoiceField``) into the
    object, and should return a string suitable for representing it. For
    example::

        from django.forms import ModelChoiceField

        class MyModelChoiceField(ModelChoiceField):
            def label_from_instance(self, obj):
                return "My Object #%i" % obj.id
+9 −0
Original line number Diff line number Diff line
@@ -183,6 +183,9 @@ the ``default_validators`` attribute.
Simple validators can be used to validate values inside the field, let's have
a look at Django's ``SlugField``::

    from django.forms import CharField
    from django.core import validators

    class SlugField(CharField):
        default_validators = [validators.validate_slug]

@@ -252,6 +255,8 @@ we want to make sure that the ``recipients`` field always contains the address
don't want to put it into the general ``MultiEmailField`` class. Instead, we
write a cleaning method that operates on the ``recipients`` field, like so::

    from django import forms

    class ContactForm(forms.Form):
        # Everything as before.
        ...
@@ -289,6 +294,8 @@ common method is to display the error at the top of the form. To create such
an error, you can raise a ``ValidationError`` from the ``clean()`` method. For
example::

    from django import forms

    class ContactForm(forms.Form):
        # Everything as before.
        ...
@@ -321,6 +328,8 @@ here and leaving it up to you and your designers to work out what works
effectively in your particular situation. Our new code (replacing the previous
sample) looks like this::

    from django import forms

    class ContactForm(forms.Form):
        # Everything as before.
        ...
+3 −0
Original line number Diff line number Diff line
@@ -201,6 +201,7 @@ foundation for custom widgets.

        .. code-block:: python

            >>> from django import forms
            >>> name = forms.TextInput(attrs={'size': 10, 'title': 'Your name',})
            >>> name.render('name', 'A name')
            u'<input title="Your name" type="text" name="name" value="A name" size="10" />'
@@ -249,6 +250,8 @@ foundation for custom widgets.
        :class:`~datetime.datetime` value into a list with date and time split
        into two separate values::

            from django.forms import MultiWidget
            
            class SplitDateTimeWidget(MultiWidget):

                # ...
Loading