Django

Code

Changeset 6273

Show
Ignore:
Timestamp:
09/15/07 05:12:05 (10 months ago)
Author:
mtredinnick
Message:

Fixed #5387 -- Added is_multipart method to forms. Original patch from Petr Marhhoun. Tests and documentation from Murkt.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r6269 r6273  
    196196    masonsimon+django@gmail.com 
    197197    Manuzhai 
     198    Petr Marhoun <petr.marhoun@gmail.com> 
    198199    Petar Marić <http://www.petarmaric.com/> 
    199200    Nuno Mariz <nmariz@gmail.com> 
  • django/trunk/django/newforms/forms.py

    r6156 r6273  
    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        for field in self.fields.values(): 
     221            if field.widget.needs_multipart_form: 
     222                return True 
     223        return False 
     224 
    215225class Form(BaseForm): 
    216226    "A collection of Fields, plus their associated data." 
  • django/trunk/django/newforms/widgets.py

    r5819 r6273  
    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): 
     
    121122class FileInput(Input): 
    122123    input_type = 'file' 
     124    needs_multipart_form = True 
    123125 
    124126    def render(self, name, value, attrs=None): 
  • django/trunk/docs/newforms.txt

    r6196 r6273  
    776776    # Unbound form with a image field 
    777777    >>> f = ContactFormWithMugshot() 
     778 
     779Testing for multipart forms 
     780~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     781 
     782If you're writing some reusable views or templates, you may not know ahead of 
     783time whether your form is a multipart form or not. The ``is_multipart()`` 
     784method tells you if the form requires multipart encoding for submission:: 
     785 
     786    >>> f = ContactFormWithMugshot() 
     787    >>> f.is_multipart() 
     788    True 
     789 
     790In a template, this sort of code could be useful:: 
     791 
     792    {% if form.is_multipart %} 
     793      <form enctype="multipart/form-data" method="post" action="/foo/"> 
     794    {% else %} 
     795      <form method="post" action="/foo/"> 
     796    {% endif %} 
     797    {% form %} 
     798    </form> 
    778799 
    779800Subclassing forms 
  • django/trunk/tests/regressiontests/forms/tests.py

    r6173 r6273  
    38573857<p>Comment: <input type="text" name="comment" /></p> 
    38583858 
     3859################################# 
     3860# Test multipart-encoded 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