Fixed #11782 -- Added some Sphinx metadata to the file uploads documentation. Thanks to timo for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12562 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-02-24 13:57:46 +00:00
parent 6f9ba54bf5
commit de1ff3e51d
1 changed files with 19 additions and 16 deletions

View File

@ -9,15 +9,15 @@ File Uploads
.. versionadded:: 1.0 .. versionadded:: 1.0
When Django handles a file upload, the file data ends up placed in When Django handles a file upload, the file data ends up placed in
``request.FILES`` (for more on the ``request`` object see the documentation for :attr:`request.FILES <django.http.HttpRequest.FILES>` (for more on the
:ref:`request and response objects <ref-request-response>`). This document ``request`` object see the documentation for :ref:`request and response objects
explains how files are stored on disk and in memory, and how to customize the <ref-request-response>`). This document explains how files are stored on disk
default behavior. and in memory, and how to customize the default behavior.
Basic file uploads Basic file uploads
================== ==================
Consider a simple form containing a ``FileField``:: Consider a simple form containing a :class:`~django.forms.FileField`::
from django import forms from django import forms
@ -25,14 +25,17 @@ Consider a simple form containing a ``FileField``::
title = forms.CharField(max_length=50) title = forms.CharField(max_length=50)
file = forms.FileField() file = forms.FileField()
A view handling this form will receive the file data in ``request.FILES``, which A view handling this form will receive the file data in
is a dictionary containing a key for each ``FileField`` (or ``ImageField``, or :attr:`request.FILES <django.http.HttpRequest.FILES>`, which is a dictionary
other ``FileField`` subclass) in the form. So the data from the above form would containing a key for each :class:`~django.forms.FileField` (or
:class:`~django.forms.ImageField`, or other :class:`~django.forms.FileField`
subclass) in the form. So the data from the above form would
be accessible as ``request.FILES['file']``. be accessible as ``request.FILES['file']``.
Note that ``request.FILES`` will only contain data if the request method was Note that :attr:`request.FILES <django.http.HttpRequest.FILES>` will only
``POST`` and the ``<form>`` that posted the request has the attribute contain data if the request method was ``POST`` and the ``<form>`` that posted
``enctype="multipart/form-data"``. Otherwise, ``request.FILES`` will be empty. the request has the attribute ``enctype="multipart/form-data"``. Otherwise,
``request.FILES`` will be empty.
Most of the time, you'll simply pass the file data from ``request`` into the Most of the time, you'll simply pass the file data from ``request`` into the
form as described in :ref:`binding-uploaded-files`. This would look form as described in :ref:`binding-uploaded-files`. This would look
@ -54,16 +57,16 @@ something like::
form = UploadFileForm() form = UploadFileForm()
return render_to_response('upload.html', {'form': form}) return render_to_response('upload.html', {'form': form})
Notice that we have to pass ``request.FILES`` into the form's constructor; this Notice that we have to pass :attr:`request.FILES <django.http.HttpRequest.FILES>`
is how file data gets bound into a form. into the form's constructor; this is how file data gets bound into a form.
Handling uploaded files Handling uploaded files
----------------------- -----------------------
The final piece of the puzzle is handling the actual file data from The final piece of the puzzle is handling the actual file data from
``request.FILES``. Each entry in this dictionary is an ``UploadedFile`` object :attr:`request.FILES <django.http.HttpRequest.FILES>`. Each entry in this
-- a simple wrapper around an uploaded file. You'll usually use one of these dictionary is an ``UploadedFile`` object -- a simple wrapper around an uploaded
methods to access the uploaded content: file. You'll usually use one of these methods to access the uploaded content:
``UploadedFile.read()`` ``UploadedFile.read()``
Read the entire uploaded data from the file. Be careful with this Read the entire uploaded data from the file. Be careful with this