美好的一天!创建了一个单独的页面来更改密码,当您输入密码并重复输入并单击“更改密码”按钮时,出现重复的键值违反唯一约束“ core_user_username_key”的错误 详细信息:密钥(用户名)=已经存在 如何解决此错误?
forms.py
class CallcenterPasswordChange(forms.ModelForm):
password1 = forms.CharField(widget=forms.PasswordInput(), label='Новый пароль')
password2 = forms.CharField(widget=forms.PasswordInput(), label='Повтор нового пароля')
def clean(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='Повтор нового пароля не совпадает',
)
return self.cleaned_data
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'password1', 'password2')
views.py
class CallcenterPasswordChangeView(AbsCallcenterView):
template_name = 'callcenter/password_change.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
patient_pk = kwargs.get('patient_pk')
patient = get_object_or_404(Patient, pk=patient_pk)
initial_data = model_to_dict(patient.user)
context['form'] =CallcenterPasswordChange(initial=initial_data)
context['patient_pk'] = patient_pk
context['patient'] = patient
return context
def post(self, request, **kwargs):
context = super().get_context_data(**kwargs)
patient_pk = kwargs.get('patient_pk')
patient = get_object_or_404(Patient, pk=patient_pk)
form = CallcenterPasswordChange(request.POST)
context['form_user'] = form
context['patient'] = patient
if form.is_valid():
form.save()
else:
return render(request, template_name=self.template_name, context=context)
答案 0 :(得分:0)
这根本不是您要做的。这是要创建一个全新的用户。
此外,password1
和password2
字段与模型中的实际密码字段没有关联;此外,没有什么可用来对密码进行哈希处理的。
您真正想要的是只有这两个字段的标准(非模型)表单,然后设置当前用户的密码。
所以形式就是:
class CallcenterPasswordChange(forms.Form):
password1 = forms.CharField(widget=forms.PasswordInput(), label='Новый пароль')
password2 = forms.CharField(widget=forms.PasswordInput(), label='Повтор нового пароля')
def clean(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='Повтор нового пароля не совпадает',
)
return self.cleaned_data
在视图中,您将执行以下操作:
form = CallcenterPasswordChange(request.POST)
if form.is_valid():
request.user.set_password(form.cleaned_data['password1'])
request.user.save()
return redirect('somewhere')
但是,您还应该注意Django已包含一个密码更改表单并已经查看。
答案 1 :(得分:0)
问题解决了!
class CallcenterPasswordChangeView(AbsCallcenterView):
template_name = 'callcenter/password_change.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
patient_pk = kwargs.get('patient_pk')
patient = get_object_or_404(Patient, pk=patient_pk)
initial_data = model_to_dict(patient.user)
context['form'] = CallcenterPasswordChange(initial=initial_data)
context['patient'] = patient
return context
def post(self, request, **kwargs):
context = super().get_context_data(**kwargs)
patient = Patient.objects.get(pk=context['patient_pk'])
form = CallcenterPasswordChange(request.POST)
context['form'] = form
if form.is_valid():
patient.user.set_password(form.cleaned_data['password1'])
patient.user.save()
success_url = reverse_lazy('callcenter:patients')
return HttpResponseRedirect(success_url)