Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Django form retain a file after failing validation

Tags:

form = AddItemForm(request.POST, request.FILES)  if form.is_valid()     do_stuff  return render_to_response(blah.html, {'form':form}) 

Now form will have the error information along with the original values of the fields, but it does not retain a selected file How do I keep the selected file if the form fails validation?

like image 854
Steve Avatar asked Jun 22 '10 23:06

Steve


People also ask

How do I extend a validation error in Django?

The to_python() method on a Field is the first step in every validation. It coerces the value to a correct datatype and raises ValidationError if that is not possible. This method accepts the raw value from the widget and returns the converted value.

How do I make my Django form valid?

Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data. The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

How do I validate a form in Django?

Fortunately, Django form validation is very easy to do. Django comes with lots of useful form validation tools right out of the box, which make building forms, managing form state and doing form validation simple and quick. In this article, we'll build a simple Django form and look at three ways that Django can help you validate form data.

How to use required in a field using Django?

Let’s check how to use required in a field using a project. When set to a particular value the option used appends some validations to the field as required by the developer. Let’s try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field.

How does the save() method of a Django form work?

The save () of a django form is used to save the data to the database models. But our form does not quiet match with the User model we have used it with. Hence, we need to change how the save () method works. If we modify nothing in the save () method, conceptually (not how it exactly works) it would look something like this.

How to check the status of a form field in Django?

We can check them in the Django shell which we can start with: Once the shell is up and running, do the following. We have only checked the status of the first_name form field. All the other fields are also similar. There is only one validator associated with the first_name field and that is MaxLengthValidator.


1 Answers

The problem with what you want to do is that, for security reasons, browsers will not allow a file input box to have a pre-selected value on page load. This is true even if it is simply preserving the value from a previous instance of the same page. There is nothing Django can do to change this.

If you want to avoid asking the user to re-select and re-upload the file, you will need to save the uploaded file even when validation fails, and then replace the file input field with something to indicate that you already have the data. You would also probably also want a button that runs some JavaScript to re-enable the file field if the user wants to put in a different file. I don't think Django comes with any machinery for this, so you'll have to code it yourself.

like image 70
Walter Mundt Avatar answered Oct 04 '22 16:10

Walter Mundt