On Thu, 2007-07-19 at 08:02 +0000, james_027 wrote:
> hi,
> 
> i've been trying to get started with django and i am stock in this
> problem and found out that there's a conflict in login() method. here
> is my view code
> 
> from django.http import HttpResponse, HttpResponseRedirect
> from django.shortcuts import render_to_response
> from django.contrib.auth import authenticate, login
> 
> 
> def index(request):
>     if not request.user.is_authenticated():
>         return render_to_response('login.htm', {'from':request.path})
>     return render_to_response('index.htm')
> 
> def login(request):
>     if request.method == 'POST':
>         user = authenticate(username=request.POST['username'],
> password=request.POST['password'])
>         if user is not None:
>             if user.is_active:
>                 #return HttpResponse('user authenticated')
>                 fixlogin(request, user)
>                 return HttpResponseRedirect('/main/')
>             else:
>                 return HttpResponse('user not active')
>         else:
>             return HttpResponse('authenticate fail')
>     return render_to_response('login.htm')
> 
> The problem is an error saying login() method accept only one
> parameter, where I follow this documentation (http://
> www.djangoproject.com/documentation/0.96/authentication/#how-to-log-a-user-in).
> After many times of trail and error I thought of changing this import
> from django.contrib.auth import authenticate, login to from
> django.contrib.auth import authenticate, login as fixlogin() and use
> fixlogin(request, user)
> 
> Is this already fix? I am just using 0.96.

There's no bug in Django here. The problem is that you import
django.contrib.auth.login into the local namespace *and* decide to
define another method called login also in the local namespace. Since
you can only have one thing of each name in the namespace, confusion
arises and the last thing trying to use that name (your method) wins.

You'll notice in the documentation you refer to that the method defined
in the local file is not called login(), for example.

This is just normal Python practice -- one thing per name.

The standard solution is to do

        from django.contrib import auth
        
and then refer to auth.login() in your login() method. That explicitly
avoids the name clash. Alternatives include picking a name other than
"login" for your method or import auth.login under some other name, as
you did.

Regards,
Malcolm

-- 
Success always occurs in private and failure in full view. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to