Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change a Django form field value before saving?

if request.method == 'POST':     userf = UsersModelForm(request.POST)     username = userf.data['username']     password = userf.data['password']     passwordrepeat = userf.data['passwordrepeat']     email = userf.data['email'] 

I tried this:

    tempSalt = bcrypt.gensalt()     password = bcrypt.hashpw(password,tempSalt)     passwordrepeat = bcrypt.hashpw(passwordrepeat,tempSalt)      userf.data['password'] = password     userf.data['passwordrepeat'] = passwordrepeat 

But i got error. How can i change the value of userf.data['password'] and userf.data['passwordrepeat'] before saving?

Error:

AttributeError at /register  This QueryDict instance is immutable  Request Method:     POST Request URL:    http://127.0.0.1:8000/register Django Version:     1.3.1 Exception Type:     AttributeError Exception Value:      This QueryDict instance is immutable  Exception Location:     /usr/local/lib/python2.6/dist-packages/django/http/__init__.py in _assert_mutable, line 359 Python Executable:  /usr/bin/python Python Version:     2.6.6 Python Path:      ['/home/user1/djangoblog',  '/usr/lib/python2.6',  '/usr/lib/python2.6/plat-linux2',  '/usr/lib/python2.6/lib-tk',  '/usr/lib/python2.6/lib-old',  '/usr/lib/python2.6/lib-dynload',  '/usr/local/lib/python2.6/dist-packages',  '/usr/lib/python2.6/dist-packages',  '/usr/lib/python2.6/dist-packages/gst-0.10',  '/usr/lib/pymodules/python2.6',  '/usr/lib/pymodules/python2.6/gtk-2.0'] 
like image 246
shibly Avatar asked Jan 19 '12 11:01

shibly


People also ask

How do I override a django form?

You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form class. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form.

How do I set default value in django?

To set default form values with Django Python, we create the form instance with the default values in the constructor arguments. Or we can put the default values in the form definition. to create a JournalForm instance with the initial argument set to a dictionary with the initial form values.

How do you make a field non editable in django?

The disabled boolean argument, when set to True , disables a form field using the disabled HTML attribute so that it won't be editable by users. Even if a user tampers with the field's value submitted to the server, it will be ignored in favor of the value from the form's initial data.

What does save () do in django?

Creating objects To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.


1 Answers

If you need to do something to the data before saving, just create a function like:

def clean_nameofdata(self):     data = self.cleaned_data['nameofdata']     # do some stuff     return data 

All you need is to create a function with the name **clean_***nameofdata* where nameofdata is the name of the field, so if you want to modify password field, you need:

def clean_password(self): 

if you need to modify passwordrepeat

def clean_passwordrepeat(self): 

So inside there, just encrypt your password and return the encrypted one.

I mean:

def clean_password(self):     data = self.cleaned_data['password']     # encrypt stuff     return data 

so when you valid the form, the password would be encrypted.

like image 123
Jesus Rodriguez Avatar answered Oct 05 '22 23:10

Jesus Rodriguez