Ticket #5387: form-is-multipart-test-docs.diff

File form-is-multipart-test-docs.diff, 2.9 KB (added by murkt, 17 years ago)

Patch with tests and documentation for rev 6268.

  • django/newforms/forms.py

     
    212212        """
    213213        return self.cleaned_data
    214214
     215    def is_multipart(self):
     216        """
     217        Returns True if the form needs to be multipart-encrypted, i.e. it has
     218        FileInput. Otherwise, False.
     219        """
     220        # python 2.5 syntax
     221        # return all(field.widget.needs_multipart_form for field in self.fields.values())
     222        for field in self.fields.values():
     223            if field.widget.needs_multipart_form:
     224                return True
     225        return False
     226
    215227class Form(BaseForm):
    216228    "A collection of Fields, plus their associated data."
    217229    # This is a separate class from BaseForm in order to abstract the way
  • django/newforms/widgets.py

     
    2424
    2525class Widget(object):
    2626    is_hidden = False          # Determines whether this corresponds to an <input type="hidden">.
     27    needs_multipart_form = False # Determines does this widget need multipart-encrypted form
    2728
    2829    def __init__(self, attrs=None):
    2930        if attrs is not None:
     
    120121
    121122class FileInput(Input):
    122123    input_type = 'file'
     124    needs_multipart_form = True
    123125
    124126    def render(self, name, value, attrs=None):
    125127        return super(FileInput, self).render(name, None, attrs=attrs)
  • docs/newforms.txt

     
    776776    # Unbound form with a image field
    777777    >>> f = ContactFormWithMugshot()
    778778
     779If you're writing some generic views and templates, you can check
     780that your form needs proper ``enctype`` with the help from ``is_multipart()``::
     781
     782    >>> f = ContactFormWithMugshot()
     783    >>> f.is_multipart()
     784    True
     785
    779786Subclassing forms
    780787-----------------
    781788
  • tests/regressiontests/forms/tests.py

     
    38563856<div class="errorlist"><div class="error">This field is required.</div></div>
    38573857<p>Comment: <input type="text" name="comment" /></p>
    38583858
     3859#################################
     3860# Test multipart-encrypted form #
     3861#################################
     3862
     3863>>> class FormWithoutFile(Form):
     3864...     username = CharField()
     3865>>> class FormWithFile(Form):
     3866...     username = CharField()
     3867...     file = FileField()
     3868>>> class FormWithImage(Form):
     3869...     image = ImageField()
     3870
     3871>>> FormWithoutFile().is_multipart()
     3872False
     3873>>> FormWithFile().is_multipart()
     3874True
     3875>>> FormWithImage().is_multipart()
     3876True
     3877
    38593878"""
    38603879
    38613880__test__ = {
Back to Top