Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: login() takes 1 positional argument but 2 were given

I have written a login view using build-in auth ,django auth.login() gives above error my code with error code o 500

from rest_framework.response import Response from rest_framework import status from rest_framework.decorators import api_view from django.contrib.auth.models import User from django.contrib.auth import authenticate,logout,login   @api_view(['POST']) def register(request):     user=User.objects.create_user(username=request.POST['username'],email=request.POST['email'],password=request.POST['password'])     return Response({'ok':'True'},status=status.HTTP_201_CREATED)  @api_view(['POST']) def login(request):     user=authenticate(         username=request.POST['username'],         password=request.POST['password']     )     if user is not None:         login(request,user)         return Response({'ok':'True'},status=status.HTTP_200_OK)     else:         return Response({'ok':'False'},status=status.HTTP_401_UNAUTHORIZED) 
like image 885
sabari rangan Avatar asked Sep 04 '16 12:09

sabari rangan


People also ask

How do you fix takes 1 positional argument but 2 were given?

You can solve the TypeError: method() takes 1 positional argument but 2 were given by adding an argument named self to your method definition as a first positional argument. But when calling it, you skip the name, so you'd define method(self, arg) but call method(arg) .

How do I login as user in Django?

Django by default will look within a templates folder called registration for auth templates. The login template is called login. html . Create a new directory called templates and within it another directory called registration .

Is authenticated Django?

Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. This section of the documentation explains how the default implementation works out of the box, as well as how to extend and customize it to suit your project's needs.


2 Answers

Your view has the same name as the auth login function, so it is hiding it. Change the view name, or import the function under a different name eg from django.contrib.auth import login as auth_login.

like image 72
Daniel Roseman Avatar answered Sep 18 '22 19:09

Daniel Roseman


You have a function def login and a library import with the same name.

What you need is to change one of them.

Example :

- def user_login  // import login as Login_process  
like image 37
Ali.M.Kamel Avatar answered Sep 18 '22 19:09

Ali.M.Kamel