File tree Expand file tree Collapse file tree 2 files changed +10
-0
lines changed Expand file tree Collapse file tree 2 files changed +10
-0
lines changed Original file line number Diff line number Diff line change @@ -716,6 +716,8 @@ form data *and* file data::
716716Testing for multipart forms
717717~~~~~~~~~~~~~~~~~~~~~~~~~~~
718718
719+ .. method:: Form.is_multipart
720+
719721If you're writing reusable views or templates, you may not know ahead of time
720722whether your form is a multipart form or not. The ``is_multipart()`` method
721723tells you whether the form requires multipart encoding for submission::
Original file line number Diff line number Diff line change @@ -197,6 +197,14 @@ context variable ``form``. Here's a simple example template::
197197The form only outputs its own fields; it is up to you to provide the surrounding
198198``<form>`` tags and the submit button.
199199
200+ If your form includes uploaded files, be sure to include
201+ ``enctype="multipart/form-data"`` in the ``form`` element. If you wish to write
202+ a generic template that will work whether or not the form has files, you can
203+ use the :meth:`~django.forms.Form.is_multipart` attribute on the form::
204+
205+ <form action="/contact/" method="post"
206+ {% if form.is_multipart %}enctype="multipart/form-data"{% endif %}>
207+
200208.. admonition:: Forms and Cross Site Request Forgery protection
201209
202210 Django ships with an easy-to-use :doc:`protection against Cross Site Request
You can’t perform that action at this time.
0 commit comments