繁体   English   中英

Django authenticate方法返回None

[英]Django authenticate method return None

我已经阅读了很多有关此问题的主题,但仍然无法解决问题。

我正在通过Tango With Django教程( 第9部分 )进行操作,并遇到了奇怪的问题。

创建用户时,无法使用authenticate方法获取用户对象。 GitHub项目:( 链接)

从角度来看,这是我的注册功能:

def register(request):
    # A boolean value for telling the template whether the registration was
    # successful.
    # Set to False initially. Code changes value to True when registration
    # succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information
        # Note that we make use of both UserForm and UserProfileForm
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object
            user.set_password(user.set_password)
            user.save()

            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set
            # commit=False
            # This delays saving the model until we're ready to avoid
            # integrity problems.
            profile = profile_form.save(commit=False)
            profile.user = user

            # Did the user provide a profile picture?
            # If so, we need to get it from the input form and put it in the
            # UserProfile
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            # Now we save the UserProfile model instance
            profile.save()

            # Update our variable to tell the template registration was
            # successful
            registered = True

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also shown to the user
        else:
            print user_form.errors, profile_form.errors

    # Not a HTTP POST, so we render out form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    context_dict = {
        'user_form': user_form,
        'profile_form': profile_form,
        'registered': registered
    }
    # Render the template depending on the context.
    return render(request, 'rango/register.html', context_dict)

user_login函数的视图:

def user_login(request):

    # If the request is a HTTP POST, try to pull out the relevant information
    if request.method == 'POST':
        # Gather the username and password provided by the user.
        # This information is obtained from the login form.
        # We use request.POST.get('<variable>') as opposed to
        # request.POST['variable'], because the
        # request.POST.get('<variables>') return None, if the value does not
        # exist, while the request.POST['<variable>'] will raise
        # key error exception
        username = request.POST.get('username')
        password = request.POST.get('password')

        # Use Django's machinery to attempt to see if the username/password
        # combination is valid - a User object is returned if it is.
        user = authenticate(username=username, password=password)
        # user = User.objects.get(username='user1')
        print user

        # If we have a User object, the details are correct.
        # If None (Python's way of representing the absence of a value), no
        # user with matching credentials was found.
        if user:
            # Is the account active? It could have been disabled
            if user.is_active:
                # If the account is valid and active, we can log the user in.
                # We'll send the user back to the homepage
                login(request, user)
                return redirect('rango:index')
            else:
                # An inactive account was user - no logging in
                return HttpResponse("Your Rango account is disabled.")
        else:
            # Bad login details were provided. So we can't log the user in.
            print "Invalid login details: {0}, {1}".format(username, password)
            return HttpResponse("Invalid login details supplied")

    # The request is not a HTTP POST, so display the login form.
    # This scenario would most likely be a HTTP GET.
    else:
        # No context variables to pass to the template system, hence
        # the blank dictionary object..
        return render(request, 'rango/login.html')

这是探戈与Django中的代码

user.set_password(user.password)
user.save()

而你的是

user.set_password(user.set_password)
user.save()

尝试将set_password替换为password

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM