本文介绍了Django用户创建成功,但authenticate()返回None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个自定义页面来创建一个新用户.用户已成功创建,但是,如果我尝试使用正确的凭据登录,则身份验证将返回None.

I wrote a custom page to create a new user. The user gets successfully created, however if I try to login with the correct credentials, authentication is returning None.

如果我进入Django Admin,则可以为刚创建的用户创建一个新密码,然后登录成功.有什么想法吗?

If I go into the Django Admin, I can create a new password for the user I just created and then login is successful. any ideas?

创建用户功能:

def create_account(request):
if request.method == "POST":
    # Default User Fields
    username = request.POST['username']
    password = request.POST['password']
    email = request.POST['email']
    first_name = request.POST['first_name']
    last_name = request.POST['last_name']


    try:
        user = User.objects.create_user(username,email,password,first_name=first_name, last_name=last_name)
        user.save()
        user_profile = models.User_Profile.objects.create_user_profile(user)
        user_profile.save()
        response = {'success': True}
        return HttpResponse(json.dumps(response))
    except Exception, e:
        response = {'success': False}
        return HttpResponse(json.dumps(response))

登录功能:

def site_login(request):
#TODO: GET THIS WORKING
if request.method == "POST":
    user = authenticate(username=request.POST['username'], password=request.POST['password'])
    if user is not None:
        if user.is_active:
            login(request, user)
            response = {'success':True}
            return HttpResponse(json.dumps(response))
        else:
            response = {'success':False,'message':'Account has been disabled.'}
            return HttpResponse(json.dumps(response))
    else:
        response = {'success':False,'message':'Invalid username or password.'}
        return HttpResponse(json.dumps(response))

推荐答案

创建用户时,您需要使用 set_password ( https://docs.djangoproject.com/zh-CN/1.9/ref/contrib/auth/#django.contrib.auth.models.User.set_password ).尝试做:

You need to use set_password when creating a user (https://docs.djangoproject.com/en/1.9/ref/contrib/auth/#django.contrib.auth.models.User.set_password). Try doing:

def create_account(request):
    if request.method == "POST":
        # Default User Fields
        username = request.POST.get('username')
        password = request.POST.get('password')
        email = request.POST.get('email')
        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')

        try:
            # illustrating it out for you
            user = User()
            user.username = username
            user.email = email
            user.set_password(password)
            user.first_name = first_name
            user.last_name = last_name
            user.save()
            user_profile = models.User_Profile.objects.create_user_profile(user)
            user_profile.save()
            response = {'success': True}
            return HttpResponse(json.dumps(response))
        except Exception, e:
            response = {'success': False}
            return HttpResponse(json.dumps(response))

这篇关于Django用户创建成功,但authenticate()返回None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 16:38