我正在浏览一个Django项目,我们没有从提供的默认Django User模型开始。现在,回到实施User模型已经为时已晚。
我拥有的用户有一组字段,包括电子邮件,密码,名字,姓氏等。我创建了一个页面来编辑用户配置文件,我能够成功编辑除密码字段以外的任何字段。我尝试创建一个单独的页面只是为了用自己的表单更改密码,但即使这样,当我尝试保存新密码值时也没有任何反应。
models.py
class User(models.Model):
First_Name = models.CharField(max_length=20)
Last_Name = models.CharField(max_length=20)
Password = models.CharField(max_length=20)
Email = models.EmailField(primary_key=True)
Address_Line1 = models.CharField(max_length=100)
Address_Line2 = models.CharField(max_length=100)
City = models.CharField(max_length=20)
State = models.CharField(max_length=2,validators=[State_validator])
ZIP_Code = models.IntegerField(validators=[Zip_Code_validator])
Country = models.CharField(max_length=100)
Phone_Number = models.CharField(max_length = 20,validators=[Numbers_validator])
Pickup_Arrangment = models.CharField(max_length=100)
Preferred_Contact = models.CharField(max_length=100)
def __str__(self):
return self.First_Name + self.Last_Name
forms.py
class PersonalManagementForm(forms.ModelForm):
class Meta:
model = models.users.User
fields = ['First_Name','Last_Name','Email','Password','Address_Line1','Address_Line2','City','State','ZIP_Code','Country','Phone_Number','Pickup_Arrangment', 'Preferred_Contact' ]
widgets = {
'First_Name': TextInput(attrs={'required': True}),
'Last_Name': TextInput(attrs={'required': True}),
'Email': TextInput(attrs={'required': True}),
'Password': PasswordInput(attrs={'required': True}),
'Address_Line1': TextInput(attrs={'required': True}),
'Address_Line2': TextInput(attrs={'required': False}),
'City': TextInput(attrs={'required': True}),
'State': TextInput(attrs={'required': True}),
'ZIP_Code': TextInput(attrs={'required': True}),
'Country': TextInput(attrs={'required': True}),
'Phone_Number': TextInput(attrs={'required': True}),
'Pickup_Arrangment': TextInput(attrs={'required': True}),
'Preferred_Contact': TextInput(attrs={'required': True})
}
def __init__(self, *args, **kwargs):
super(PersonalManagementForm, self).__init__(*args, **kwargs)
views.py
def personalmanagemnet(request):
myuser = is_logged_in(request)
if not myuser:
return HttpResponseRedirect("/login/")
personalmanagemnet_form = PersonalManagementForm(request.POST or None, instance=myuser)
if personalmanagemnet_form.is_valid():
personalmanagemnet_form.save()
return HttpResponseRedirect('/dashboard')
update_context = {
'update_form': personalmanagemnet_form,
}
return render(request, 'personalmanagemnet.html', update_context )
堆栈溢出中的几乎所有答案都对应于使用Django User模型,因此在这种情况下它没有帮助。如何在django中手动更新密码字段?