views:

591

answers:

4

I have a Django form that allows a user to change their password. I find it confusing on form error for the fields to have the *'ed out data still in them.

I've tried several methods for removing form.data, but I keep getting a This QueryDict instance is immutable exception message.

Is there a proper way to clear individual form fields or the entire form data set from clean()?

A: 

If you need extra validation using more than one field from a form, override the .clean() method. But if it's just for one field, you can create a clean_field_name() method.

http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms-validation

Igor Sobreira
Igor, I know how to validate the form. The issue is when the validation fails, I want to have the form field returned empty, not with the data the user provided.
ChrisW
Igor is on the right track, though, ChrisW. IMO, you should use the clean() method to validate the two passwords - and if they are not correct clear the self.data fields <at that point>, before raising a validation error.
pithyless
Did this actually work? I'm doing something similar (subclass the form; call super().clean() and, if it raises and exception, try to modify `self.data` and re-raise) but get the `This QueryDict instance is immutable` `AttributeError`.
thsutton
A: 

I guess you need to use JavaScript to hide or remove the text from the container.

Prashanth
There certainly has to be a way to do this server-side... depending on client-side JavaScript is a pretty messy way to accomplish this.
ChrisW
A: 

Someone showed me how to do this. This method is working for me:

post_vars = {}
post_vars.update(request.POST)
form = MyForm(post_vars, auto_id='my-form-%s')
form.data['fieldname'] = ''
form.data['fieldname2'] = ''

ChrisW
This works, but it's not very clean since you're doing it at the views level. IMO, you should subclass the default login form and replace the clean() method as Igor suggested. That is, first validate they are correct; clear the relevant self.data fields, and lastly raise a ValidationError.
pithyless
A: 

Can't you just delete the password data from the form's cleaned_data during validation?

See the Django docs for custom validation (especially the second block of code).

LaundroMat