Ticket #1653: forms.txt.diff

File forms.txt.diff, 1.8 KB (added by asmodai@…, 18 years ago)

Patch to document FileField and ImageField's use in forms a bit

  • docs/forms.txt

     
    581581    the executable specified in the ``JING_PATH`` setting (see the settings_
    582582    document for more details).
    583583
     584FileField, ImageField and forms
     585===============================
     586
     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.
     590
     591Say that in the model definition there is a declaration for::
     592
     593    file = model.FileField('/path/to/upload/location')
     594
     595Normally this would be used in a template form like the following::
     596
     597    <p><label for="file">File:</label> {{ form.file }}</p>
     598
     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
     601expected.
     602
     603To get this behaviour a little change to the template is needed to get the
     604following::
     605
     606    <p><label for="file">File:</label> {{ form.file }}{{ form.file_file }}</p>
     607
     608If you would reload the page it should now show a file upload text field
     609complete with a browse button.
     610
     611Also make sure to use ``form.file_file`` with validation and not
     612``form.file``.
     613
     614In the view make sure that the ``update(request.FILES)`` method is called
     615after the object has been created with the ``request.POST.copy()`` call, as
     616such::
     617
     618    new_data = request.POST.copy()
     619    new_data.update(request.FILES)
     620
    584621.. _`generic views`: http://www.djangoproject.com/documentation/generic_views/
    585622.. _`models API`: http://www.djangoproject.com/documentation/model_api/
    586623.. _settings: http://www.djangoproject.com/documentation/settings/
Back to Top