[英]UserCreationForm doesn't save the password commit=False
I create a registration form on HTML ajax pull the data and send it to UserCreationForm via a function in views.py.我在 HTML ajax 上创建了一个注册表单,提取数据并通过 views.py 中的 function 将其发送到 UserCreationForm。 But when I check the user I realized the password is not saved.
但是当我检查用户时,我意识到密码没有保存。 I am using ajax so I took the data per field.
我正在使用 ajax 所以我获取了每个字段的数据。 And in the view function, I had to add the field values one by one.
而在视图 function 中,我不得不一一添加字段值。 I used set_password but didn't help.
我使用了 set_password 但没有帮助。
Any help will very appreciated thank you My Form:任何帮助将不胜感激谢谢我的表格:
class RegisterForm(UserCreationForm):
username = forms.CharField(max_length=100 , widget=forms.TextInput(
attrs={'class': "form-control"}))
email = forms.EmailField(max_length=100, widget=forms.EmailInput())
password1 = forms.CharField(widget=forms.PasswordInput(
attrs={'class': "form-control"}))
password2 = forms.CharField(widget=forms.PasswordInput(
attrs={'class': "form-control"}))
email.widget.attrs.update({'class': 'form-control'})
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
My View:我的观点:
def home(request):
register_form = RegisterForm()
workshop_form = WorkshopCreateForm
if request.method == 'POST':
register_form = RegisterForm(request.POST)
username = request.POST.get('username')
print(username)
email = request.POST.get('email')
password1 = request.POST.get('pass1')
password2 = request.POST.get('pass2')
if register_form.is_valid():
pended_form = register_form.instance
pended_form.username = username
pended_form.email = email
if password1 == password2:
pended_form.set_password(password1)
pended_form.save()
ctx = {
'username': pended_form.username,
'password': pended_form.password,
'created': True,
'success': True,
'status':'You created your account',
}
return JsonResponse(ctx)
else:
ctx = {
'username': '',
'password': '',
'created': False,
'success': False,
'status':'error',
'msg': _('Passwords do not match')
}
return JsonResponse(ctx)
#! TODO: Create a function and import login func from there
else:
ctx = {
'created': False,
'success': False,
'status':'error',
'msg': _('You need to enter a valid username and email')
}
return JsonResponse(ctx)
return render(request, 'web/home.html', {'register_form':register_form})
just create a new user.只需创建一个新用户。
User.create.objects(username, email)
user.set_password(password)
user.save()
you can do this in your view您可以在您的视图中执行此操作
I needed to use the RegisterForm() directly without parsing.我需要直接使用 RegisterForm() 而不进行解析。 I don't need to parse the data.
我不需要解析数据。 I just put the data inside of RegisterForm() so the problem is solved.
我只是将数据放在 RegisterForm() 中,这样问题就解决了。
if request.method == 'POST' and request.POST.get("operation") == "register":
register_form = RegisterForm(request.POST)
if register_form.is_valid():
register_form.save()
return JsonResponse(ctx)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.