im trying to login using django.contrib.auth it giving me error
whenever im trying to login it gives me error below
TypeError at /login/user
login() takes 1 positional argument but 2 were given
this is my login template and url work perfectly from login/user
<div class="container border border-2 border-info rounded-2 p-3" style="width: 400px;margin-top: 100px; margin-bottom: 200px;">
<form action="user" method="post">
{% csrf_token %}
<h3 class="p-3">
<small class="text-muted">Login To Your Account</small>
</h3>
<div class="mb-3 form-floating">
<input type="text" class="form-control" id="InputUsername" aria-describedby="emailHelp" name="username" placeholder="Enter Username">
<div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
<label for="InputUsername" >Username</label>
</div>
<div >
<input type="password" id="InputPassword" name="password" placeholder="Enter Password">
<label for="InputPassword" >Password</label>
</div>
<div >
<input type="checkbox" id="exampleCheck1">
<label for="exampleCheck1">Remember me</label>
</div>
<button type="submit" >Login</button><a href="/register" >Create Account</a>
</form>
</div>
urls.py look like this
from store.settings import MEDIA_ROOT, MEDIA_URL
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',views.home,name="home"),
path('login/',views.login,name="login"),
path('register/',views.register,name="register"),
path('register/user',views.registeruser,name="registeruser"),
path('login/user',views.handlelogin,name="loginuser"),
path('/user/logout',views.handlelogout,name="logout")
] static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
views.py my handle login function
def handlelogin(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
return redirect('')
else:
# Return an 'invalid login' error message
return redirect('login')
def handlelogout(request):
logout(request)
return redirect(' ')
i dont get what im doing wrong can someone tell me please
CodePudding user response:
Based on your urls.py
, you defined a login
function, this thus means that if you call login(request, user)
, it will call the view function, not the login function from the django.contrib.auth
module.
You can import it with a different name, for example:
from django.contrib.auth import login as authlogin
# ⋮
def login(request):
# ⋮
def handlelogin(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
authlogin(request, user)
# Redirect to a success page.
return redirect('')
else:
# Return an 'invalid login' error message
return redirect('login')