我使用Django提供的用户模型。一切正常,但在我尝试制作email
字段unique
后进行了一次更改。
我的forms.py
:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
User._meta.get_field('email')._unique = True
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )
我添加到forms.py
以便将电子邮件字段unique
添加到的代码行是:
User._meta.get_field('email')._unique = True
我的views.py
:
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
user.refresh_from_db()
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=4096,
backend=default_backend()
)
public_key = private_key.public_key()
pem_private_key = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
user.profile.public_key = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
raw_password = form.cleaned_data.get('password1').encode()
user.profile.salt = os.urandom(16)
kdf = Scrypt(
salt=user.profile.salt,
length=32,
n=2 ** 20,
r=8,
p=1,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(raw_password))
f = Fernet(key)
user.profile.encrypted_private_key = f.encrypt(pem_private_key)
user.save()
current_site = get_current_site(request)
subject = 'Activate Your MySite Account'
message = render_to_string('accounts/account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
user.email_user(subject, message)
return redirect('account_activation_sent')
else:
form = SignUpForm()
return render(request, 'accounts/signup.html', {'form': form})
这里是我注册用户的地方,我还添加了一个配置文件模型,我为用户创建了一些密钥对,所有密钥对都按预期工作。我还发送了一个电子邮件激活,以激活用户。
我的models.py
:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
email_confirmed = models.BooleanField(default=False)
encrypted_private_key = models.CharField(max_length=500, blank=True)
public_key = models.CharField(max_length=30, blank=True)
salt = models.CharField(max_length=16, blank=True)
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
在这里,我使用信号创建用户配置文件。
我的html文件:
{% extends 'base.html' %}
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
{% endblock %}
当我添加该行代码以在email
auth_user
中生成unique
字段时,我必须makemigrations
然后migrate
。 email
字段确实变为unique
,当我尝试使用现有电子邮件注册时,我收到错误代码。但是在观察数据库时,我注意到电子邮件保存在last_name
列上,这真的很奇怪。
我试图通过删除该行代码并再次应用迁移来扭转这种情况,但同样的事情又发生了。
在我将电子邮件字段设为唯一之前,一切都按预期工作。
答案 0 :(得分:0)
您是否有理由重新发明密码加密过程?通常这样做通常不如Django的原生解决方案安全。
您是否考虑过创建一个使用唯一电子邮件的自定义用户模型(绕过表单中的魔术)? 另外,我建议你研究django-registration或django-allauth(我更喜欢all-auth)。这些可以有效地完成你为你做的所有事情。
https://github.com/pennersr/django-allauth https://github.com/ubernostrum/django-registration