I have setuped django-rest-framework and its basic users/groups api as described here: http://www.django-rest-framework.org/. While I can see users and groups via the api, I can't log in. There is /api-auth/login page and whenever I insert un & passwd and submit it just redirects to the same page without any renderred message. I wonder what I am missing. How can I "log in" into the API.
2 Answers
Django rest framework support different methods of authentification. Not sure what you mean by /api-auth/. There is no such standard url in this framework. Please check documentation to see what methods of authentication are possible and how to do it.
4 Comments
clime
If you go on the provided link in the question and search for 'api-auth', you will find out what I meant. Anyway it was my fault that I incorrectly replaced Authentication backend for my own.
Aldarund
Well i did the search. There is no api-auth on the link i provided. As well as there is no api-auth in the source code of django-rest-framework ( that what i was checked in first place)
clime
well that is strange, this is the url django-rest-framework.org and I can locate this paragraph: If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root urls.py file.
urlpatterns = patterns('', ... url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) )Aldarund
Well, its main page not my link. And its just example of url, not something that coded into framework, so it can by anything.
It was caused by me employing a custom auth backend:
# settings.py
AUTHENTICATION_BACKENDS = (
'web.auth_backends.EmailAuthBackend',
)
# auth_backends.py
class EmailAuthBackend(object):
def authenticate(self, email=None, password=None):
try:
user = User.objects.get(email__iexact=email)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Basically it was being passed a keyword "username" but it accepted "email" instead. So it raised an exception that was catched in django's contrib.auth.authentificate and user was not able to log in. I solved it by substituting "email" for "username".
1 Comment
SlackOverflow
Remember to mark this as answer if you found a solution.