Ticket #1653: forms.txt.2.diff

File forms.txt.2.diff, 2.2 KB (added by asmodai@…, 12 years ago)

Updated patch, explaining enctype as well.

  • docs/forms.txt

    581581    the executable specified in the ``JING_PATH`` setting (see the settings_
    582582    document for more details).
     584FileField, ImageField and forms
     587Where most, if not all, model fields only require a simple addition in the
     588form of ``{{ form.fieldname }}`` to a template, the situation is a bit
     589different for the FileField and ImageField fields.
     591Say that in the model definition there is a declaration for::
     593    file = model.FileField('/path/to/upload/location')
     595Normally this would be used in a template form like the following::
     597    <p><label for="file">File:</label> {{ form.file }}</p>
     599The first thing that is noticeable when the page is opened in a browser is
     600that there is no typical text field with a browse button, as would be
     603To get this behaviour a little change to the template is needed to get the
     606    <p><label for="file">File:</label> {{ form.file }}{{ form.file_file }}</p>
     608If you would reload the page it should now show a file upload text field
     609complete with a browse button.
     611The HTML standard specifies the ``enctype`` attribute to the ``form`` element
     612in order to select the content type used in submitting. The default is
     613``application/x-www-form-urlencoded``, the format that uses ampersands in the
     614URL to separate key-value pairs as well as some other encoding tricks. This
     615the wrong encoding type to use when working with forms that will use file
     616uploads. The ``form`` element must get an ``enctype`` attribute with its value
     617set to ``multipart/form-data``.
     619Also make sure to use ``form.file_file`` with validation and not
     622In the view make sure that the ``update(request.FILES)`` method is called
     623after the object has been created with the ``request.POST.copy()`` call, as
     626    new_data = request.POST.copy()
     627    new_data.update(request.FILES)
    584629.. _`generic views`: http://www.djangoproject.com/documentation/generic_views/
    585630.. _`models API`: http://www.djangoproject.com/documentation/model_api/
    586631.. _settings: http://www.djangoproject.com/documentation/settings/
Back to Top