我找不到问题的解决方案。
该站点要求用户在其他字段中注册。并通过电子邮件授权。我通过一个附加模型添加了这些字段:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.IntegerField('Телефон', null=True, blank=True)
country = models.CharField('Страна', max_length=100, null=True, blank=True)
city = models.CharField('Город', max_length=100, null=True, blank=True)
adress = models.CharField('Улица и дом', max_length=300, null=True, blank=True)
zip_code = models.IntegerField('Почтовый индекс', null=True, blank=True)
birth_day = models.DateField('День рождения', null=True, blank=True)
avatar = models.ImageField('Аватар', upload_to='avatar', null=True, blank=True)
agreement = models.BooleanField('Согласие на обработку', default=True)
comment = models.TextField('Комментарии', max_length=3000, null=True, blank=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
def admin_display(self):
return self.user.last_name + ' ' + self.user.first_name
接下来,我制作了 2 个表格在模板中显示:
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
exclude = ('comment', 'register_date', 'avatar', 'password', )
widgets = {
'phone': forms.NumberInput(attrs={'placeholder': '9XXXXXXXXX', 'class': 'form-control', 'id': 'phone'}),
'country': forms.TextInput(attrs={'placeholder': 'Россия', 'class': 'form-control', 'id': 'country'}),
'city': forms.TextInput(attrs={'placeholder': 'Москва', 'class': 'form-control', 'id': 'city'}),
'adress': forms.TextInput(attrs={'placeholder': 'Ленина 25', 'class': 'form-control', 'id': 'adress'}),
'zip_code': forms.TextInput(attrs={'placeholder': '101000', 'class': 'form-control', 'id': 'zip_code'}),
'agreement': forms.CheckboxInput(attrs={'class': 'form-check-input', 'id': 'flexSwitchCheckChecked'}),
'birth_day': DateInput(attrs={'class': 'form-control', 'id': 'birth_day'}),
}
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'password',)
widgets = {
'first_name': forms.TextInput(attrs={'placeholder': 'Иван', 'class': 'form-control', 'id': 'first_name'}),
'last_name': forms.TextInput(attrs={'placeholder': 'Петров', 'class': 'form-control', 'id': 'last_name'}),
'email': forms.EmailInput(attrs={'placeholder': 'ivan_petrov@email.ru', 'class': 'form-control', 'id': 'email'}),
'password': forms.PasswordInput(attrs={'placeholder': 'Пароль', 'class': 'form-control', 'id': 'password'}),
}
这是视图中的内容:
def registration(request):
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.username = request.POST['email']
instance.set_password(request.POST['password'])
instance.save()
return redirect('registration')
else:
form = UserForm(request.POST)
profile_form = ProfileForm(request.POST)
context = {
'form': form,
'profile_form': profile_form,
}
return render(request, 'user_registration.html', context)
else:
form = UserForm()
profile_form = ProfileForm()
context = {
'form': form,
'profile_form': profile_form,
}
return render(request, 'user_registration.html', context)
我没有显示用户名字段。相反,我在那里添加了一封电子邮件。用户已创建。由于某种原因,只有密码没有加密。并且该用户无法登录。我收到登录名/密码匹配错误。而且我找不到登录功能的示例。请帮我解决一下这个。