Django Login - Missing 1 Required Positional Argument

TypeError at /login login() missing 1 required positional argument: 'User'

The login view should not contain a User parameter, in fact for the given view, you do not need to import the User model at all. Furthermore in order to login, you should use the login(…) method [Django-doc]. Since you already have a method named login, you should import it under a different name:

from django.contrib.auth import login as auth_login

# no User parameter
def login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
auth_login(request, user)
return render(request, 'welcome.html',)
else:
messages.info(request , 'invalid credentials')
return render(request, 'login.html')
else:
return render(request, 'welcome.html')


Note: In case of a successful POST request, you should make a redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.

Getting this error in django "login() missing 1 required positional argument: 'user' "

You’ve got the import wrong. You need to import the login view:

from django.contrib.auth.views import login

At the moment you have imported the login function that actually logs the user in.

The login view you are using is deprecated in Django 1.11. You can switch to the new class-based LoginView:

from django.contrib.auth.views import LoginView

url(r'^login/$', LoginView.as_view(), name='login'),

Get_queryset() missing 1 required positional argument: 'request' when i'm trying to filter objects by logged in user

You can do this

class FacilityListView(ListAPIView):
permission_classes = [IsAuthenticated]
serializer_class = FacilitySerializer


def get_queryset(self, request):
admin_uid = self.request.user.id
return Facility.objects.filter(admin_uid=admin_uid)

Django custom user error when registering: get_session_auth_hash() missing 1 required positional argument: 'self' (new user can otherwise log in)

You've got a typo in login. You are using User which is the class instead of user:

login(request, User) # --> should be login(request, user)


Related Topics



Leave a reply



Submit