如何在保存之前更改Django表单字段值?

时间:2022-12-09 13:54:49
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?

但是我有错误。如何更改userf的值。数据(“密码”)和userf。数据(“passwordrepeat”)前保存吗?

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']

5 个解决方案

#1


29  

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:

您只需创建一个名为**clean_*** *nameofdata*的函数,其中nameofdata是字段的名称,因此如果要修改密码字段,您需要:

def clean_password(self):

if you need to modify passwordrepeat

如果您需要修改passwordrepeat。

def clean_passwordrepeat(self):

So inside there, just cript your password and return the cripted one.

因此,在这里,只需输入您的密码并返回已标记的密码。

I mean:

我的意思是:

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

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

因此,当您对窗体有效时,密码就会被敲入。

#2


9  

See the documentation for the save() method

请参阅save()方法的文档

if request.method == 'POST':
    userf = UsersModelForm(request.POST)
    new_user = userf.save(commit=False)

    username = userf.cleaned_data['username']
    password = userf.cleaned_data['password']
    passwordrepeat = userf.cleaned_data['passwordrepeat']
    email = userf.cleaned_data['email']

    new_user.password = new1
    new_user.passwordrepeat = new2

    new_user.save()

#3


5  

You will have problems if you need to fill form from POST, change any form field value and render form again. Here is solution for it:

如果您需要从POST中填充表单,更改任何表单字段值并再次呈现表单,那么您将会遇到问题。以下是解决方法:

class StudentSignUpForm(forms.Form):
  step = forms.IntegerField()

  def set_step(self, step):
    data = self.data.copy()
    data['step'] = step
    self.data = data

And then:

然后:

form = StudentSignUpForm(request.POST)
if form.is_valid() and something():
  form.set_step(2)
  return  render_to_string('form.html', {'form': form})

#4


5  

Override _clean methods and put your checks in them. You can modify cleaned_data from there.

重写_clean方法,并将您的检查放在其中。您可以从那里修改cleaned_data。

E.g:

例句:

def clean_password(self):
    new1 = self.cleaned_data['password']
    return new1

Every fields in the form will have a field_name_clean() method created automatically by Django. This method is called when you do form.is_valid().

表单中的每个字段都将使用Django自动创建的field_name_clean()方法。在执行form.is_valid()时调用此方法。

#5


0  

The problem with the previous solutions is, that it will not work if validation fails. In order to avoid validation, you can use the instance:

以前的解决方案的问题是,如果验证失败,它将不起作用。为了避免验证,可以使用实例:

instance = form.instance
instance.user = request.user
instance.save()

But be careful, this does not check is_valid(). If you want to do that, you can instantiate the form with the new values:

但是要小心,这并不检查is_valid()。如果您想这样做,您可以用新值实例化表单:

# NOT TESTED, NOT SURE IF THIS WORKS...
form = MyForm(instance=instance)
if form.is_valid():
    form.save()

#1


29  

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:

您只需创建一个名为**clean_*** *nameofdata*的函数,其中nameofdata是字段的名称,因此如果要修改密码字段,您需要:

def clean_password(self):

if you need to modify passwordrepeat

如果您需要修改passwordrepeat。

def clean_passwordrepeat(self):

So inside there, just cript your password and return the cripted one.

因此,在这里,只需输入您的密码并返回已标记的密码。

I mean:

我的意思是:

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

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

因此,当您对窗体有效时,密码就会被敲入。

#2


9  

See the documentation for the save() method

请参阅save()方法的文档

if request.method == 'POST':
    userf = UsersModelForm(request.POST)
    new_user = userf.save(commit=False)

    username = userf.cleaned_data['username']
    password = userf.cleaned_data['password']
    passwordrepeat = userf.cleaned_data['passwordrepeat']
    email = userf.cleaned_data['email']

    new_user.password = new1
    new_user.passwordrepeat = new2

    new_user.save()

#3


5  

You will have problems if you need to fill form from POST, change any form field value and render form again. Here is solution for it:

如果您需要从POST中填充表单,更改任何表单字段值并再次呈现表单,那么您将会遇到问题。以下是解决方法:

class StudentSignUpForm(forms.Form):
  step = forms.IntegerField()

  def set_step(self, step):
    data = self.data.copy()
    data['step'] = step
    self.data = data

And then:

然后:

form = StudentSignUpForm(request.POST)
if form.is_valid() and something():
  form.set_step(2)
  return  render_to_string('form.html', {'form': form})

#4


5  

Override _clean methods and put your checks in them. You can modify cleaned_data from there.

重写_clean方法,并将您的检查放在其中。您可以从那里修改cleaned_data。

E.g:

例句:

def clean_password(self):
    new1 = self.cleaned_data['password']
    return new1

Every fields in the form will have a field_name_clean() method created automatically by Django. This method is called when you do form.is_valid().

表单中的每个字段都将使用Django自动创建的field_name_clean()方法。在执行form.is_valid()时调用此方法。

#5


0  

The problem with the previous solutions is, that it will not work if validation fails. In order to avoid validation, you can use the instance:

以前的解决方案的问题是,如果验证失败,它将不起作用。为了避免验证,可以使用实例:

instance = form.instance
instance.user = request.user
instance.save()

But be careful, this does not check is_valid(). If you want to do that, you can instantiate the form with the new values:

但是要小心,这并不检查is_valid()。如果您想这样做,您可以用新值实例化表单:

# NOT TESTED, NOT SURE IF THIS WORKS...
form = MyForm(instance=instance)
if form.is_valid():
    form.save()