Ticket #5387: form-is-multipart-test-docs.diff
File form-is-multipart-test-docs.diff, 2.9 KB (added by , 17 years ago) |
---|
-
django/newforms/forms.py
212 212 """ 213 213 return self.cleaned_data 214 214 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 215 227 class Form(BaseForm): 216 228 "A collection of Fields, plus their associated data." 217 229 # This is a separate class from BaseForm in order to abstract the way -
django/newforms/widgets.py
24 24 25 25 class Widget(object): 26 26 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 27 28 28 29 def __init__(self, attrs=None): 29 30 if attrs is not None: … … 120 121 121 122 class FileInput(Input): 122 123 input_type = 'file' 124 needs_multipart_form = True 123 125 124 126 def render(self, name, value, attrs=None): 125 127 return super(FileInput, self).render(name, None, attrs=attrs) -
docs/newforms.txt
776 776 # Unbound form with a image field 777 777 >>> f = ContactFormWithMugshot() 778 778 779 If you're writing some generic views and templates, you can check 780 that your form needs proper ``enctype`` with the help from ``is_multipart()``:: 781 782 >>> f = ContactFormWithMugshot() 783 >>> f.is_multipart() 784 True 785 779 786 Subclassing forms 780 787 ----------------- 781 788 -
tests/regressiontests/forms/tests.py
3856 3856 <div class="errorlist"><div class="error">This field is required.</div></div> 3857 3857 <p>Comment: <input type="text" name="comment" /></p> 3858 3858 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() 3872 False 3873 >>> FormWithFile().is_multipart() 3874 True 3875 >>> FormWithImage().is_multipart() 3876 True 3877 3859 3878 """ 3860 3879 3861 3880 __test__ = {