Ignoring the malformed code, will the call to authenticate() even work 
without username? According to the docs,
https://docs.djangoproject.com/en/1.7/topics/auth/default/#django.contrib.auth.authenticate

It takes credentials in the form of keyword arguments, for the default 
> configuration this is username and password, and it returns a User 
> <https://docs.djangoproject.com/en/1.7/ref/contrib/auth/#django.contrib.auth.models.User>
>  
> object if the password is valid for the given username.
>
>
If someone wanted to change the default behavior, I would expect them to at 
least override the function. OP doesn't seem to have done this, so it 
shouldn't even work. (Please correct me if I'm missing something)

-Abraham V.

On Friday, January 16, 2015 at 2:37:13 AM UTC+5:30, Matt Cooper wrote:
>
> 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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/c7e27779-311b-43f4-b66d-d3548becdc26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to