I am on Chapter 10 User Login & Auth of the tutorial by Dave Gray. When I try to login after registering a new user I get this TypeError at /users/login/
login() takes 1 positional argument but 2 were given.
Exception Type: TypeError
Exception Value:
login() takes 1 positional argument but 2 were given
Exception Location: C:\Users\ZACKAMATA\Documents\Learn Python\Django\Dave Gray\Lesson01\myproject\users\views.py, line 25, in login
Raised during: users.views.login
This is my views.py file
from django.shortcuts import render, redirect # type: ignore
from django.contrib.auth.forms import UserCreationForm # type:ignore
from django.contrib.auth.forms import AuthenticationForm # type:ignore
from django.contrib.auth import login # type:ignore
# Create your views here.
def register(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
login(request, form.save())
return redirect("posts:list")
else:
form = UserCreationForm()
return render(request, "users/register.html", {"form": form})
def login(request):
if request.method == "POST":
form = AuthenticationForm(data=request.POST)
if form.is_valid():
login(request, form.get_user())
return redirect("posts:list")
else:
form = AuthenticationForm()
return render(request, "users/login.html", {"form": form})
In comparison this is the views.py file from the tutorial resources
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import login
# Create your views here.
def register_view(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
login(request, form.save())
return redirect("posts:list")
else:
form = UserCreationForm()
return render(request, "users/register.html", { "form": form })
def login_view(request):
if request.method == "POST":
form = AuthenticationForm(data=request.POST)
if form.is_valid():
login(request, form.get_user())
return redirect("posts:list")
else:
form = AuthenticationForm()
return render(request, "users/login.html", { "form": form })
You will notice that on lines 2 and 3 of the views.py file, I have the following import commands
from django.contrib.auth.forms import UserCreationForm # type:ignore
from django.contrib.auth.forms import AuthenticationForm # type:ignore
This because when I import both UserCreationForm and AuthenticationForm on the same line I get this when I save
from django.contrib.auth.forms import (
UserCreationForm,
AuthenticationForm,
) # type:ignore
Notice the parentheses.