Ticket #11782: 11782.diff
File 11782.diff, 3.4 KB (added by , 15 years ago) |
---|
-
docs/topics/http/file-uploads.txt
9 9 .. versionadded:: 1.0 10 10 11 11 When Django handles a file upload, the file data ends up placed in 12 ``request.FILES`` (for more on the ``request`` object see the documentation for 13 :ref:`request and response objects <ref-request-response>`). This document 14 explains how files are stored on disk and in memory, and how to customize the 15 default behavior.12 :attr:`request.FILES <django.http.HttpRequest.FILES>` (for more on the 13 ``request`` object see the documentation for :ref:`request and response objects 14 <ref-request-response>`). This document explains how files are stored on disk 15 and in memory, and how to customize the default behavior. 16 16 17 17 Basic file uploads 18 18 ================== 19 19 20 Consider a simple form containing a ``FileField``::20 Consider a simple form containing a :class:`~django.forms.FileField`:: 21 21 22 22 from django import forms 23 23 … … 25 25 title = forms.CharField(max_length=50) 26 26 file = forms.FileField() 27 27 28 A view handling this form will receive the file data in ``request.FILES``, which 29 is a dictionary containing a key for each ``FileField`` (or ``ImageField``, or 30 other ``FileField`` subclass) in the form. So the data from the above form would 28 A view handling this form will receive the file data in 29 :attr:`request.FILES <django.http.HttpRequest.FILES>`, which is a dictionary 30 containing a key for each :class:`~django.forms.FileField` (or 31 :class:`~django.forms.ImageField`, or other :class:`~django.forms.FileField` 32 subclass) in the form. So the data from the above form would 31 33 be accessible as ``request.FILES['file']``. 32 34 33 Note that ``request.FILES`` will only contain data if the request method was 34 ``POST`` and the ``<form>`` that posted the request has the attribute 35 ``enctype="multipart/form-data"``. Otherwise, ``request.FILES`` will be empty. 35 Note that :attr:`request.FILES <django.http.HttpRequest.FILES>` will only 36 contain data if the request method was ``POST`` and the ``<form>`` that posted 37 the request has the attribute ``enctype="multipart/form-data"``. Otherwise, 38 ``request.FILES`` will be empty. 36 39 37 40 Most of the time, you'll simply pass the file data from ``request`` into the 38 41 form as described in :ref:`binding-uploaded-files`. This would look … … 54 57 form = UploadFileForm() 55 58 return render_to_response('upload.html', {'form': form}) 56 59 57 Notice that we have to pass ``request.FILES`` into the form's constructor; this58 i s how file data gets bound into a form.60 Notice that we have to pass :attr:`request.FILES <django.http.HttpRequest.FILES>` 61 into the form's constructor; this is how file data gets bound into a form. 59 62 60 63 Handling uploaded files 61 64 ----------------------- 62 65 63 66 The final piece of the puzzle is handling the actual file data from 64 ``request.FILES``. Each entry in this dictionary is an ``UploadedFile`` object 65 -- a simple wrapper around an uploaded file. You'll usually use one of these 66 methods to access the uploaded content:67 :attr:`request.FILES <django.http.HttpRequest.FILES>`. Each entry in this 68 dictionary is an ``UploadedFile`` object -- a simple wrapper around an uploaded 69 file. You'll usually use one of these methods to access the uploaded content: 67 70 68 71 ``UploadedFile.read()`` 69 72 Read the entire uploaded data from the file. Be careful with this