AttributeError at / accounts / login / 'User' does not have the attribute 'user'

I am new to django and trying to start user authentication. I have a very simple login and browse form, but I get an error:

AttributeError at /accounts/login/ 'User' object has no attribute 'user'

I am confused because I am not trying to access User.user.
I know that this should be something in the first else statement, because the authenticated user simply redirects to "/", because it should
look like this:

def login(request):
  if request.user.is_authenticated():
    return HttpResponseRedirect("/")
  else:
    if request.method == 'POST':
      username = request.POST['username']
      password = request.POST['password']
      user = authenticate(username=username, password=password)
      if user is not None:
        if user.is_active:
          login(user)
          return HttpResponseRedirect("/")
      return HttpResponse("There was an error logging you in")
    else:
      return render_to_response("registration/login.html", 
                                 context_instance=RequestContext(request))

The error occurs in views.py, line 15: if request.user.is_authenticated ():

+3
source share
2 answers

Your view function is called login, and it requires one parameter request.

11 login(user). , , , django.contrib.auth, , , . Python , : , login, .

, , . ( : request.user, request , , user , , , .)

, , from django.contrib import auth, auth.login(user) .

+4
+1

All Articles