[英]Django User with that username already exists
My aim - Trying to create an authentication system in Django, and allows user to signup again with same username if their account is not activated.我的目标- 尝试在 Django 中创建身份验证系统,并允许用户在未激活帐户时使用相同的用户名再次注册。
If an user try to register with a certain username and that username already exists then update that user with this current user.如果用户尝试使用某个用户名注册并且该用户名已经存在,则使用该当前用户更新该用户。
My Approach - I have created a form using "UserCreationForm" and defining my own methods to validate the form, and allows the user even if username already exists but user.is_active = False.我的方法- 我使用“UserCreationForm”创建了一个表单并定义了我自己的方法来验证表单,并允许用户即使用户名已经存在但 user.is_active = False。
Code代码
forms.py forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import User
class SignupForm(UserCreationForm):
email = forms.EmailField(max_length=200, help_text='Required')
name = forms.CharField()
institution = forms.CharField()
def clean_username(self):
username = self.cleaned_data.get('username')
user = None
try:
try:
user = User.objects.get(username=username)
print("is user active username", user.is_active)
except ObjectDoesNotExist as e:
pass
except Exception as e:
raise e
if not user:
pass
elif not user.is_active:
pass
else:
raise forms.ValidationError("This Username Already Exists")
except Exception as e:
raise e
return username
class Meta:
model = User
fields = ('username', 'email', 'institution', 'password1', 'password2')
views.py视图.py
from .forms import SignupForm
def regUser(form):
'''
It will save Django user and student.
It recieves form.cleaned_data as argument
'''
print("reg user line 1")
user = User.objects.create_user(username=form['username'],
email=form['email'],
password=form['password1'],
is_active=False,
)
# notice is_active = False, becuase we will activate account if user will
# verify the email
user.save()
student = Student(user=user, name=form['name'], institution=form['institution'])
student.save()
return user
def signup(request):
if request.user.is_authenticated:
return redirect('accounts:home')
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
user = regUser(form.cleaned_data)
current_site = get_current_site(request)
message = render_to_string('accounts/acc_active_email.html', {
'user':user, 'domain':current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
mail_subject = 'Activate your account.'
to_email = form.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
return render(request, 'accounts/signup.html', {'email_sent': True})
else:
for field in form:
for error in field.errors:
messages.error(request, error)
return redirect("accounts:signup")
return render(request, 'accounts/signup.html')
models.py模型.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
subject = models.CharField(max_length=250, default="default_value")
class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
name = models.CharField(max_length=250, default="default_value")
institution = models.CharField(max_length=250, default="default_value")
def save(self):
super().save()
def __str__(self):
return self.user.username
My problem - I am getting error " A user with that username already exists" I don't why and from where this error is coming when I am trying to created a new user with already registered username but it account is not activated yet (user.is_active = False).我的问题- 我收到错误“具有该用户名的用户已经存在”当我尝试使用已注册的用户名创建新用户但帐户尚未激活时,我不知道为什么以及从何处出现此错误(用户.is_active = 假)。
As much I know clean_fields functions are called to validate the form when we use "form.is_valid()" in our views.py, But I have already overridden the "clean_username" according to my need so why I am getting this error?据我所知,当我们在views.py中使用“form.is_valid()”时,会调用clean_fields函数来验证表单,但是我已经根据需要覆盖了“clean_username”,为什么会出现这个错误? and from where it is generated?
它是从哪里生成的? and how to solve this?
以及如何解决这个问题?
Also I would like to mention when user is registered it is also registered "student" that's why I am using "regUser" function.另外我想提一下,当用户注册时,它也是注册的“学生”,这就是我使用“regUser”function 的原因。 Also I read some other questions similar to this on stackoverflow this and this some answer was that inherit the form from "forms.Form" not from "UserCreationForm" but why can anyone explain?
另外我在stackoverflow上阅读了一些与此类似的其他问题, 这个答案是从“forms.Form”而不是“UserCreationForm”继承表单,但为什么有人可以解释?
Please not mark this question as already answered I have gone through all questions and it was not helpful.请不要将此问题标记为已回答我已经解决了所有问题,但没有帮助。
The User model you defined inherited the AbstactUser
, and username
is defined there.您定义的用户 model 继承了
AbstactUser
,并且在那里定义了username
。
class AbstractUser(AbstractBaseUser, PermissionsMixin):
"""
An abstract base class implementing a fully featured User model with
admin-compliant permissions.
Username and password are required. Other fields are optional.
"""
username_validator = UnicodeUsernameValidator()
username = models.CharField(
_('username'),
max_length=150,
unique=True,
help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[username_validator],
error_messages={
'unique': _("A user with that username already exists."),
},
)
...
The attribute unique
for username
field means that username must be unique among all users , not only active users . username
字段的属性unique
意味着用户名在所有用户中必须是唯一的,而不仅仅是活动用户。 (ref.https://docs.djangoproject.com/en/3.1/ref/models/fields/#unique ) (参考https://docs.djangoproject.com/en/3.1/ref/models/fields/#unique )
If what you want is to allow users to have the same username among inactive users, set unique=False.如果您想要允许用户在非活动用户中使用相同的用户名,请设置 unique=False。
But, if 'inactive' means 'discarded' in your User
model, I would recommend changing user's username
to garbage value when the user is being inactivated.但是,如果“非活动”在您的
User
model 中意味着“丢弃”,我建议在用户被非激活时将用户的username
更改为垃圾值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.