Django:authenticate()除了一个case之外,不返回任何值
所以我在我的项目中陷入了困境。我正在尝试创建用户,并按照文档中的建议使用OneToOnefield方法扩展了用户模型,以创建用户配置文件。创建用户时没有问题,因为所有详细信息都正确存储在auth_user和appname_userprofile表中 我遇到的问题是,当我尝试使用auth_user表中存储的任何用户登录时。除了一个案例(我在创建userProfile模型之前创建的)。因此,在我的auth_user表中,这一个案例的过程是编码的,而其余案例的过程是纯文本的 下面是我处理视图的函数-Django:authenticate()除了一个case之外,不返回任何值,django,django-models,django-views,django-authentication,Django,Django Models,Django Views,Django Authentication,所以我在我的项目中陷入了困境。我正在尝试创建用户,并按照文档中的建议使用OneToOnefield方法扩展了用户模型,以创建用户配置文件。创建用户时没有问题,因为所有详细信息都正确存储在auth_user和appname_userprofile表中 我遇到的问题是,当我尝试使用auth_user表中存储的任何用户登录时。除了一个案例(我在创建userProfile模型之前创建的)。因此,在我的auth_user表中,这一个案例的过程是编码的,而其余案例的过程是纯文本的 下面是我处理视图的函数-
def login_view(request):
if request.method == 'POST':
print(request.body)
username = request.POST.get('id_username')
password = request.POST.get('id_password')
print(username, password)
try:
import pdb; pdb.set_trace()
user = authenticate(username=username, password=password)
print('This is ',user)
if user is not None:
login(request,user)
else:
return redirect('fileupload')
print('This is ',user).... some more code
现在,当我传递这个案例的凭据并尝试进行身份验证时。登录正常(除了我的代码在以后某个时候被卡住,因为这个用户没有任何配置文件)
当我为创建UserProfile模型后创建的用户传递凭据时。
验证(用户名=用户名,密码=密码)
函数返回
没有
我已将其包含在我的设置中。py-
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
这是我的模型-
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 UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
contact_no = models.CharField(max_length=10, null=True)
# email = models.EmailField()
department = models.CharField(max_length=25, null=True)
status = models.IntegerField(null=True)
industry_segment = models.CharField(max_length=50, null=True)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.IntegerField(null=True)
updated_at = models.DateTimeField(auto_now=True)
updated_by = models.IntegerField(null=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()
这就是我保存用户的方式-
def user_view(request):
print(request.user)
if request.is_ajax():
print(request.user)
data = json.load(request)
u_name = data['txtfirstName'] + " " + data['txtlastName']
user=User(username=u_name,password='test123',email=data['txtemailId'])
user.save()
# userprofile= UserProfile.objects.create(user=request.user)
user.userprofile.contact_no = data['txtcontactNo']
# user.userprofile.email = data['txtemailId']
user.userprofile.department = data['txtdeptvalue']
user.userprofile.status = data['txtstatusvalue']
user.userprofile.industry_segment = 'Software'
user.userprofile.created_at = datetime.datetime.now()
user.userprofile.created_by = request.user.id
user.userprofile.updated_at = datetime.datetime.now()
user.userprofile.updated_by = request.user.id
user.save()
忽略此代码的格式。。。但它正在发挥作用
有人能帮我解决身份验证问题吗?您不能创建这样的用户。密码不会被散列,因此在身份验证中永远不会匹配。您必须始终使用
create\u user
manager方法
user = User.objects.create_user(username=u_name, password='test123', email=data['txtemailId'])
您不能创建这样的用户。密码不会被散列,因此在身份验证中永远不会匹配。您必须始终使用
create\u user
manager方法
user = User.objects.create_user(username=u_name, password='test123', email=data['txtemailId'])