在任何人将其标记为重复之前,我知道有很多相似或完全相同的问题,但是没有一个解决方案似乎对我有用而且我一无所知。
像往常一样,我希望django.contrib.auth.models中的用户模型具有不区分大小写的用户名。我尝试了两种方法here。不幸的是,这些都不适合我。在完成链接中描述的方法后,是否还需要编辑我的views.py?我该怎么做以实现不区分大小写?
这是我的观点
def login_page(request):
if(request.method == 'POST'):
name = request.POST['user']
if not User.objects.filter(username=name).exists():
return render(request, 'login.html', {'error_user': 'user does not exist'})
pwd = request.POST['pwd']
user = authenticate(username=name, password=pwd)
if user is not None:
login(request, user)
return redirect('index')
else:
return render(request, 'login.html', {'error_pwd': name})
else:
return render(request, 'login.html')
以下是我的models.py:
的一部分class CustomUserManager(UserManager):
def get_by_natural_key(self, username):
case_insensitive_username_field =
'{}__iexact'.format(self.model.USERNAME_FIELD)
return self.get(**{case_insensitive_username_field: username})
class CustomUser(AbstractUser):
objects = CustomUserManager()
class ContentItem(models.Model):
upload_date = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=100, default='no title')
description = models.CharField(max_length=400, default='no description')
image = models.ImageField(upload_to='image_board/posts/',
default='null')
uploaded_by = models.ForeignKey(User, default='0')
def __str__(self):
return self.title
def get_likes(self):
no = self.like_set.all().count()
if no is not None:
return no
else:
return 0
class Profile(models.Model):
user = models.OneToOneField(User,
related_name='profile',on_delete=models.CASCADE)
#email = models.EmailField()
#first_name = models.CharField(max_length=20, blank=True)
#last_name = models.CharField(max_length=20, blank=True)
personal_info = models.TextField(blank=True)
job_title = models.CharField(max_length=100, blank=True)
department = models.CharField(max_length=100, blank=True)
location = models.CharField(max_length=100, blank=True)
expertise = models.TextField(blank=True)
phone_regex = RegexValidator(regex=r'^\+?1?\d{5,15}$', message="Phone
number must be entered in the format: '+123456'. Between 5 and 15 digits
allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=16,
blank=True)
contact_skype = models.URLField(null=True, blank=True)
contact_facebook = models.URLField(null=True, blank=True)
contact_linkedin = models.URLField(null=True, blank=True)
user_photo = models.ImageField(upload_to='../media/img',
default='../media/img/anon.png')
def __str__(self):
return self.job_title
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
编辑:对于链接中的选项1,我在进行迁移时遇到以下错误:
File "C:\Anaconda3\lib\site-packages\django-1.11-py3.4.egg\django\contrib\auth\__init__.py", line 194, in get_user_model
"AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model
'core.CustomUser' that has not been installed
选项2不会抛出任何错误,但如果我创建一个用户名为" User"并尝试使用用户名"用户"登录,它提供"用户不存在"错误。
答案 0 :(得分:1)
您可以尝试这样的事情
def login_page(request):
if(request.method == 'POST'):
name = request.POST['user']
temp_user = User.objects.filter(username__iexact=name)
if not temp_user.exists():
return render(request, 'login.html', {'error_user': 'user does not exist'})
pwd = request.POST['pwd']
user = authenticate(username=temp_user.username, password=pwd)
if user is not None:
login(request, user)
return redirect('index')
else:
return render(request, 'login.html', {'error_pwd': name})
else:
return render(request, 'login.html')
答案 1 :(得分:0)
在EJB
中,settings.py
应该是一个包含这样的字符串,AUTH_USER_MODEL
。
您是否选中了'<your_app_name>.CustomUser'
,即您应用的名称。
修改强>:
也许可以尝试更改'core'
模型,
Profile