Python 如何允许用户更改其属性?
我正试图找出如何允许用户更改他们的个人资料。我有一个Python 如何允许用户更改其属性?,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,我正试图找出如何允许用户更改他们的个人资料。我有一个用户扩展的用户档案(OneToOne) 我正在考虑更改注册视图,预先填充用户的属性,并允许他更改它们。但这可能不是好办法 你能告诉我怎么做吗 class UserForm(forms.ModelForm): password1 = forms.CharField(widget=forms.PasswordInput()) password2 = forms.CharField(widget=forms.PasswordInput
用户
扩展的用户档案
(OneToOne)
我正在考虑更改注册视图,预先填充用户的属性,并允许他更改它们。但这可能不是好办法
你能告诉我怎么做吗
class UserForm(forms.ModelForm):
password1 = forms.CharField(widget=forms.PasswordInput())
password2 = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ('username', 'email', 'password1','password2', 'first_name', 'last_name')
def clean(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return self.cleaned_data
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('telephone','marital_status','how_do_you_know_about_us')
型号.PY
class UserProfile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
# ATRIBUTY KTORE BUDE MAT KAZDY
telephone = models.CharField(max_length=40,null=True)
HOW_DO_YOU_KNOW_ABOUT_US_CHOICES = (
('coincidence',u'It was coincidence'),
('relative_or_friends','From my relatives or friends'),
)
how_do_you_know_about_us = models.CharField(max_length=40, choices=HOW_DO_YOU_KNOW_ABOUT_US_CHOICES, null=True)
MARITAL_STATUS_CHOICES = (
('single','Single'),
('married','Married'),
('separated','Separated'),
('divorced','Divorced'),
('widowed','Widowed'),
)
marital_status = models.CharField(max_length=40, choices=MARITAL_STATUS_CHOICES, null=True)
# OD KIAL STE SA O NAS DOZVEDELI
# A STAV
def __unicode__(self):
return '{} {}'.format(self.user.first_name,self.user.last_name)
def __str__(self):
return '{} {}'.format(self.user.first_name,self.user.last_name)
注册视图:
def register(request):
if request.method == 'POST':
user_form = UserForm(request.POST)
profile_form = UserProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user_form.cleaned_data['password1'])
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
return register_success(request)
else:
print user_form.errors, profile_form.errors
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render(request, "auth/registration/register.html",
context={'user_form': user_form, 'profile_form': profile_form})
编辑:
这是我尝试创建的视图,但它不会自动填充表单:
@login_required
def edit_profile(request):
myUser = request.user
user_form = UserForm(request.POST, instance=myUser)
user_profile_form = UserProfileForm(request.POST, instance=myUser)
context={'user_form': user_form,
'user_profile_form':user_profile_form}
return render(request, 'auth/profiles/my_profile.html', context=context)
在您添加的
edit_profile
视图中,您正在向表单传递一个POST请求参数。只能在POST请求中传递此参数。因此,如果请求是GET请求,请将表单更新为以下内容:
views.py
user_form = UserForm(instance=myUser)
user_profile_form = UserProfileForm(instance=myUser)
forms.py
# Something like this will only save password if data is entered in one of the password fields
def clean(self):
cleaned_data = super(UserForm, self).clean()
password1 = cleaned_data.get('password1', None)
password2 = cleaned_data.get('password2', None)
old_password = cleaned_data.get('old_password', None)
if password1 or password2:
if password1 != password2:
self._errors['password1'] = 'New Password and Confirm New Password must match.'
self._errors['password2'] = 'New Password and Confirm New Password must match.'
if not self.user.check_password(old_password):
self._errors['old_password'] = 'Your old password was entered incorrectly.'
return cleaned_data
def save(self, request):
user = self.user
if self.cleaned_data.get('password1', None):
user.set_password(self.cleaned_data.get('password1'))
update_session_auth_hash(request, user)
user.save()
return user
对于您的选择问题,您可以在为字段指定窗口小部件时将选择指定为参数。我不明白,是什么让您认为这不是一个好方法?我不知道,我认为有一些内置模块可以做到这一点。但它不起作用。我上传了一个问题。谢谢curtis,这很有效,但只是部分有效。选择表未填写。你知道有什么问题吗?活动用户已经填写了这些表单。如何排除password1和password2?而且电话也没有满。这可能是因为我将User作为属性,但在第二种形式中,应该有UserProfile。但是我如何使用用户实例获得它呢?我已经更新了我的答案,以解决选择表单字段和密码的问题。看看你能否以此作为开始。对于user_profile_表单,请记住您想要的是用户的配置文件实例,而不一定是用户本身的实例。还要注意,我在表单(未绑定字段)中添加了一个
old_password
字段。我发现在允许他们更改密码之前确认密码是一种很好的做法。提示。。。您需要遵循一对一关系的反向关系