添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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

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>
    <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 class="mb-3 form-floating">
      <input type="password" class="form-control" id="InputPassword" name="password" placeholder="Enter Password">
      <label for="InputPassword" class="form-label">Password</label>
    <div class="mb-3 form-check">
      <input type="checkbox" class="form-check-input" id="exampleCheck1">
      <label class="form-check-label" for="exampleCheck1">Remember me</label>
    <button type="submit" class="btn btn-primary">Login</button><a href="/register" class="streched-link p-3">Create Account</a>
  </form>

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

i imported authenticate , login and logout as well but login() function giving me error i mentioned above , handle login is copy pasted from django documentation still i cant resolve it, i watch videos and i did the everything same – Farman Oct 30, 2021 at 12:57

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
# &vellip;
def login(request):
    # &vellip;
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')

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.