Your if block in views.py is not well-formed. I haven't tested this but I'd
write it more like this:
# try username
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
return HttpResponseRedirect('/')
# fall-through to email
user = auth.authenticate(email=username, password=password)
if user is not None:
auth.login(request, user)
return HttpResponseRedirect('/')
# ok, neither one worked
return HttpResponseRedirect('/accounts/invalid_login')
On Wednesday, January 14, 2015 at 10:07:29 PM UTC-8, Kakar Nyori wrote:
> I have extendted the *UserCreationForm* with email and other fields, so
> that I could authenticate a user with both its username and email.
>
> forms.py:
>
>> class UserCreationForm(UserCreationForm):
>> class Meta:
>> model = User
>> fields = ('first_name', 'last_name', 'username', 'email',)
>
>
>
> views.py:
>
> def auth_view(request):
>> username = request.POST.get('username','')
>> password = request.POST.get('password','')
>> user = auth.authenticate(username=username, password=password)
>> if user is not None:
>> auth.login(request, user)
>> return HttpResponseRedirect('/')
>> elif:
>> user = auth.authenticate(email=username, password=password)
>> if user is not None:
>> auth.login(request, user)
>> return HttpResponseRedirect('/')
>> else:
>> return HttpResponseRedirect('/accounts/invalid_login')
>
>
> html:
>
> <form action="/accounts/auth/" method="post">
>> {%csrf_token%}
>> <label for="name">Email or Username:</label>
>> <input type="text" name="name" id="name" value="">
>> <label for="password">Password:</label>
>> <input type="password" name="password" id="password" value="">
>> <input type="submit" value="LOGIN">
>> </form>
>
>
>
> In the views I tried giving both the *username* and *email *as input from
> the form as *name*, and check to see if username and password
> authenticate. If not then check whether email and password authenticate.
> But its not working. How do I solve this problem? Please kindly help me.
> Thank you.
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/97740eb6-f826-4159-b770-20e66d917b20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.