Django test client returns an anonymous user after login
Tag : python , By : user118656
Date : March 29 2020, 07:55 AM
wish of those help That login won't succeed because Django expects the password to be a hash but you have saved it in plain text. Do this instead: user = User.objects.create_user(username=username, password=password)
|
Django REST Framework client.login() not working. (User created)
Date : March 29 2020, 07:55 AM
this will help Problem was in using User.objects.create() insetad of User.objects.create_superuser(), thanks to @C14L.
|
how to login a Custom User Model in django rest API framework
Tag : python , By : General Mills
Date : March 29 2020, 07:55 AM
I wish this help you I defined a Custom User Model in models.py whose name is Student. This model inherits Django User. I can sign up student correctly, but when I want to login , I get error. , Look closer at the student filtering: user = Student.objects.filter(
Q(identity_no=identity_no) |
Q(student_no=student_no)
).distinct()
class CustomUser(AbstractUser):
...
identity_no = ...
class Student(models.Model):
...
user = ...
student_no = ...
user = Student.objects.filter(
Q(user__identity_no=identity_no) | # <<<
Q(student_no=student_no)
).distinct()
|
Django REST Framework ObtainAuthToken User Login Api view
Tag : django , By : Gianluca Riccardi
Date : March 29 2020, 07:55 AM
Does that help In the docs it tell that you can override the return response of the post request in ObtainAuthToken: from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
class CustomAuthToken(ObtainAuthToken):
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data,
context={'request': request})
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return Response({
'token': token.key,
'user_id': user.pk,
'email': user.email
})
urlpatterns += [
url(r'^api-token-auth/', CustomAuthToken.as_view())
]
|
How to login a user during a unit test in Django REST Framework?
Tag : django , By : Alan Little
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Since you are using Django REST Framework you have to also use DRF's test client called APIClient instead of Django's test client. This happens automagically if you inherit from DRF's APITestCase instead of Django's TestCase. Complete example: class CheckUserViewTest(APITestCase):
def test_check_user(self):
user = User.objects.create_user('username', 'Pas$w0rd')
self.assertTrue(self.client.login(username='username', password='Pas$w0rd'))
response = self.client.get(reverse('check_user'))
self.assertEqual(response.status_code, httplib.OK)
class CheckUserViewTest(APITestCase):
def test_check_user(self):
user = User.objects.create_user('username', 'Pas$w0rd')
self.client.force_authenticate(user)
response = self.client.get(reverse('check_user'))
self.assertEqual(response.status_code, httplib.OK)
|