shi*_*bly 30 python forms django model save
if request.method == 'POST':
userf = UsersModelForm(request.POST)
username = userf.data['username']
password = userf.data['password']
passwordrepeat = userf.data['passwordrepeat']
email = userf.data['email']
Run Code Online (Sandbox Code Playgroud)
我试过这个:
tempSalt = bcrypt.gensalt()
password = bcrypt.hashpw(password,tempSalt)
passwordrepeat = bcrypt.hashpw(passwordrepeat,tempSalt)
userf.data['password'] = password
userf.data['passwordrepeat'] = passwordrepeat
Run Code Online (Sandbox Code Playgroud)
但我得到了错误.如何更改保存前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']
Run Code Online (Sandbox Code Playgroud)
Jes*_*uez 41
如果您需要在保存之前对数据执行某些操作,只需创建一个函数,如:
def clean_nameofdata(self):
data = self.cleaned_data['nameofdata']
# do some stuff
return data
Run Code Online (Sandbox Code Playgroud)
您只需要创建一个名为**clean _***nameofdata*的函数,其中nameofdata是字段的名称,因此如果要修改密码字段,则需要:
def clean_password(self):
Run Code Online (Sandbox Code Playgroud)
如果你需要修改密码重复
def clean_passwordrepeat(self):
Run Code Online (Sandbox Code Playgroud)
所以在那里,只需加密密码并返回加密密码.
我的意思是:
def clean_password(self):
data = self.cleaned_data['password']
# encrypt stuff
return data
Run Code Online (Sandbox Code Playgroud)
因此,当您对表单有效时,密码将被加密.
Bur*_*lid 11
请参阅该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()
Run Code Online (Sandbox Code Playgroud)
如果您需要从POST填写表单,更改任何表单字段值并再次呈现表单,您将遇到问题.这是解决方案:
class StudentSignUpForm(forms.Form):
step = forms.IntegerField()
def set_step(self, step):
data = self.data.copy()
data['step'] = step
self.data = data
Run Code Online (Sandbox Code Playgroud)
然后:
form = StudentSignUpForm(request.POST)
if form.is_valid() and something():
form.set_step(2)
return render_to_string('form.html', {'form': form})
Run Code Online (Sandbox Code Playgroud)
覆盖_clean
方法并将检查放入其中.你可以cleaned_data
从那里修改.
例如:
def clean_password(self):
new1 = self.cleaned_data['password']
return new1
Run Code Online (Sandbox Code Playgroud)
表单中的每个字段都有一个field_name_clean()
由Django自动创建的方法.当你这样做时,会调用此方法form.is_valid()
.
归档时间: |
|
查看次数: |
45027 次 |
最近记录: |