繁体   English   中英

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

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

我试过这个:

    tempSalt = bcrypt.gensalt()
    password = bcrypt.hashpw(password,tempSalt)
    passwordrepeat = bcrypt.hashpw(passwordrepeat,tempSalt)

    userf.data['password'] = password
    userf.data['passwordrepeat'] = passwordrepeat

但我有错误。 如何在保存前更改userf.data['password']userf.data['passwordrepeat']

错误:

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

如果您需要在保存前对数据做一些事情,只需创建一个函数,如:

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

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

def clean_password(self):

如果需要修改密码repeat

def clean_passwordrepeat(self):

所以在里面,只需加密您的密码并返回加密的密码。

我的意思是:

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

因此,当您验证表单时,密码将被加密。

请参阅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()

如果您需要从 POST 填写表单、更改任何表单字段值并再次呈现表单,您将遇到问题。 这是它的解决方案:

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

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

接着:

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

覆盖_clean方法并将您的检查放入其中 您可以从那里修改cleaned_data

例如:

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

表单中的每个字段都有一个由 Django 自动创建的field_name_clean()方法。 当您执行form.is_valid()时会调用此方法。

在视图中:

def post(self, request, *args, **kwargs):
    form = self.form_class(request.POST)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.my_field = 'value'
        obj.save()

在表格中:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM