Django的身份验证功能似乎总是没有返回,我已经检查了这个thread并更新了settigns.py文件中的身份验证后端 .

这是我用来保存正在创建的帐户的代码:

if request.method == 'POST':

    # Getting the information from the filled in UserCreationForm
    user_form = UserCreationForm(data=request.POST)

    # If the the form is valid then save the users data to the database
    # hash the password using set_password and save the user again
    # set registered to True
    if user_form.is_valid():

        user = user_form.save()

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

        registered = True

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

# Not a HTTP POST, so these forms will be blank, ready for user input.
else:
    user_form = UserCreationForm()

# Render the template depending on the context.
return render_to_response(
    'contracts/register.html',
        {'user_form': user_form, 'registered': registered}, context)

这似乎有效,因为在我在网站上创建新用户之后,访问数据库将链接到使用该用户名和加密密码的更新 .

user_from是从UserCreationForm创建的,它是:

class UserCreationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())

class Meta:
    model = User
    fields = ('username','password','first_name','last_name','email','is_staff','is_active')

这是我的user_login方法中的代码,它应该从请求中获取用户名和密码并验证组合

def user_login(request):
# context for the user's request.
context = RequestContext(request)
# form = AuthenticationForm()

# If the request is a HTTP POST
if request.method == 'POST':
    # Gather the username and password provided by the user.
    # This information is obtained from the login form.
    user = request.user

    userSubmit = user.username
    passSubmit = user.password

    #username = request.POST.get['username']
    #password = request.POST.get['password']

    user = authenticate(username = userSubmit, password=passSubmit)

    #try:
    #    user = authenticate(username=userSubmit, password=passSubmit)
    #except LockedOut:
    #    messages.error(request, 'You have been locked out because of too many login attempts. Please try again in 10 minutes.')

    # 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.
    else:
        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('home')

            else:
                # An inactive account was used - no logging in!
                messages.error(request, 'Your account is disabled.')

        else:
            messages.error(request, 'The credentials you entered are invalid.')
# Bad login details were provided. So we can't log the user in.
# The request is not a HTTP POST, so display the login form.
# This scenario would most likely be a HTTP GET.
# No context variables to pass to the template system, hence the
# blank dictionary object...
return render_to_response('administrative/login.html', {'form': form}, context)