Django “login() takes exactly 1 argument (2 given)” error

后端 未结 3 1630
轻奢々
轻奢々 2021-01-30 10:02

I\'m trying to store the user\'s ID in the session using django.contrib.auth.login . But it is not working not as expected.

I\'m getting error login() takes exac

相关标签:
3条回答
  • 2021-01-30 10:27

    One possible fix:

    from django.contrib import auth
    
    def login(request):
        # ....
        auth.login(request, user)
        # ...
    

    Now your view name doesn't overwrite django's view name.

    0 讨论(0)
  • 2021-01-30 10:38

    Your view function is also called login, and the call to login(request, user) ends up being interpreted as a attempt to call this function recursively:

    def login(request):
        ...
        login(request, user)
    

    To avoid it rename your view function or refer to the login from django.contrib.auth in some different way. You could for example change the import to rename the login function:

    from django.contrib.auth import login as auth_login
    
    ...
    auth_login(request, user)
    
    0 讨论(0)
  • 2021-01-30 10:42

    Another way:

    from django.contrib.auth import login as auth_login
    

    then call auth_login(request, user) instead of login(request, user).

    0 讨论(0)
提交回复
热议问题