Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I dont know why i receiving this error login() takes 1 positional argument but 2 were given , ive just follow this tutorial that after sign-up it will login automatic https://www.csestack.org/django-sign-up-registration-form/

def login(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            print("test")
            user = form.save()
            user.refresh_from_db()
            user.first_name = form.cleaned_data.get('first_name')
            user.last_name = form.cleaned_data.get('last_name')
            user.contact_number = form.cleaned_data.get('contact_number')
            user.email = form.cleaned_data.get('email')
            user.save()
            insert = Customer(
                user=user,
                first_name=user.first_name,
                last_name=user.last_name,
                contact_number=user.contact_number,
                email=user.email
            insert.save()
            raw_password = form.cleaned_data.get('password1')
            users = authenticate(username=user.username, password=raw_password)
            login(request, users)
            return redirect('Homepage')
    else:
        form = SignUpForm()
    return render(request, 'mysite/login.html', {'form': form})

this is the traceback

Traceback (most recent call last): File "C:\Users\clair\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) File "C:\Users\clair\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\clair\OneDrive\Desktop\Thesis\mysite\myapp\views.py", line 36, in login login(request, users)

Exception Type: TypeError at /login/ Exception Value: login() takes 1 positional argument but 2 were given

Does this answer your question? TypeError: login() takes 1 positional argument but 2 were given – Abdul Aziz Barkat Jun 18, 2022 at 13:47

A short bit of detail for you to understand what's happening here.

When you've imported Django's login method, then you went ahead and defined your own method also called login, that's what caused the issue here because of the function (your defined login method) is calling itself rather than Django's login method.

from django.contrib.auth import login  # Importing django's login method
# This is your defined method which takes one argument
def login(request):
     # Because having the same name as the Django method imported, it's calling itself instead.
     # Therefore you're getting an error here since it's calling itself and you're passing two arguments.
     login(request, users)

Therefore, as mentioned by the others, just rename your defined login method to allow the program to call the Django login method instead.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.