forms.Form.is_multipart()

Form.is_multipart() If you’re writing reusable views or templates, you may not know ahead of time whether your form is a multipart form or not. The is_multipart() method tells you whether the form requires multipart encoding for submission: >>> f = ContactFormWithMugshot() >>> f.is_multipart() True Here’s an example of how you might use this in a template: {% if form.is_multipart %} <form enctype="multipart/form-data" method="post" action="/foo/"> {% else %}

forms.Form.initial

Form.initial Use initial to declare the initial value of form fields at runtime. For example, you might want to fill in a username field with the username of the current session. To accomplish this, use the initial argument to a Form. This argument, if given, should be a dictionary mapping field names to initial values. Only include the fields for which you’re specifying an initial value; it’s not necessary to include every field in your form. For example: >>> f = ContactForm(initia

forms.Form.is_bound

Form.is_bound If you need to distinguish between bound and unbound form instances at runtime, check the value of the form’s is_bound attribute: >>> f = ContactForm() >>> f.is_bound False >>> f = ContactForm({'subject': 'hello'}) >>> f.is_bound True Note that passing an empty dictionary creates a bound form with empty data: >>> f = ContactForm({}) >>> f.is_bound True If you have a bound Form instance and want to change the data somehow,

forms.Form.is_valid()

Form.is_valid() The primary task of a Form object is to validate data. With a bound Form instance, call the is_valid() method to run validation and return a boolean designating whether the data was valid: >>> data = {'subject': 'hello', ... 'message': 'Hi there', ... 'sender': 'foo@example.com', ... 'cc_myself': True} >>> f = ContactForm(data) >>> f.is_valid() True Let’s try with some invalid data. In this case, subject is blank (an error,

forms.Form.field_order

Form.field_order New in Django 1.9. By default Form.field_order=None, which retains the order in which you define the fields in your form class. If field_order is a list of field names, the fields are ordered as specified by the list and remaining fields are appended according to the default order. Unknown field names in the list are ignored. This makes it possible to disable a field in a subclass by setting it to None without having to redefine ordering. You can also use the Form.field_or

forms.Form.has_changed()

Form.has_changed() Use the has_changed() method on your Form when you need to check if the form data has been changed from the initial data. >>> data = {'subject': 'hello', ... 'message': 'Hi there', ... 'sender': 'foo@example.com', ... 'cc_myself': True} >>> f = ContactForm(data, initial=data) >>> f.has_changed() False When the form is submitted, we reconstruct it and provide the original data so that the comparison can be done: >>&g

forms.Form.fields

Form.fields You can access the fields of Form instance from its fields attribute: >>> for row in f.fields.values(): print(row) ... <django.forms.fields.CharField object at 0x7ffaac632510> <django.forms.fields.URLField object at 0x7ffaac632f90> <django.forms.fields.CharField object at 0x7ffaac3aa050> >>> f.fields['name'] <django.forms.fields.CharField object at 0x7ffaac6324d0> You can alter the field of Form instance to change the way it is presented

forms.Form.has_error()

Form.has_error(field, code=None) This method returns a boolean designating whether a field has an error with a specific error code. If code is None, it will return True if the field contains any errors at all. To check for non-field errors use NON_FIELD_ERRORS as the field parameter.

forms.Form.errors

Form.errors Access the errors attribute to get a dictionary of error messages: >>> f.errors {'sender': ['Enter a valid email address.'], 'subject': ['This field is required.']} In this dictionary, the keys are the field names, and the values are lists of Unicode strings representing the error messages. The error messages are stored in lists because a field can have multiple error messages. You can access errors without having to call is_valid() first. The form’s data will be valida

forms.Form.errors.as_json()

Form.errors.as_json(escape_html=False) Returns the errors serialized as JSON. >>> f.errors.as_json() {"sender": [{"message": "Enter a valid email address.", "code": "invalid"}], "subject": [{"message": "This field is required.", "code": "required"}]} By default, as_json() does not escape its output. If you are using it for something like AJAX requests to a form view where the client interprets the response and inserts errors into the page, you’ll want to be sure to escape the resul