简体   繁体   中英

User is not authenticated in Django? It shows errors

I am creating a website so I finshed register page in login page, every details are correct but that is showing please verify your details I mean the else part also I put in a print statement after username and password same details are printing when I typed but not access login.

views.py

def sign_in(request):
        
    if request.method == "POST":
        username=request.POST['username']
        password=request.POST['password']
        print(username,password)
        user = authenticate(username=username,password=password)
            
        if user is not None:
            login(request,user)
            messages.success(request,"you are logged successfully ")
            return redirect("front_page")    
        else:
            messages.error(request,"please verify your details")
            return redirect("sign_in")   
    return render(request,"login.html")

def front_page(request):
    return render(request,"frontpage.html")

urls.py

 path('log_in',views.sign_in,name="sign_in"),
 path('front_page',views.front_page,name="front_page"),

html

 <div class="signup-form">
        <form action="{% url 'sign_in' %}" method="POST">
         {% csrf_token %}
         <h2 class="text-center">Login</h2>
         <p class="text-center">Please fill in this form to login your account!</p>
         <hr>
   
         <div class="form-group">
           <div class="input-group">
             <div class="input-group-prepend">
               <span class="input-group-text">
                 <span class="fa fa-user"></span>
                         </span>
             </div> 
             <input type="text" class="form-control" name="username" placeholder="Username" required="required">
           </div>
         </div> 
         
         <div class="form-group">
            <div class="input-group">
              <div class="input-group-prepend">
                <span class="input-group-text">
                  <i class="fa fa-lock"></i>
                </span>                    
              </div>
                <input type="password" class="form-control" name="password" placeholder="Password" required="required">
            </div>
          </div>

          <div class="form-group d-flex justify-content-center">
            <button type="submit" class="btn btn-primary btn-lg">Login</button>
          </div>
          <div class="text-center text-primary">Dont have a account? <a href="{% url 'register' %}">Create here </a></div>
        </form>
       
    </div>

I just want to login with username and password, but it shows please verify your details, but all the details are correct.

Any ideas?

authenticate() also requires request as an argument so it should be:

user = authenticate(request,username=username,password=password)

Your view for registering users should be like this:

def register(request):
if request.method == "POST":
    username=request.POST['username']
    password=request.POST['password']
    confirm_password=request.POST['confirm_password']
    
    if password == confirm_password:    
        if User.objects.filter(username=username).exists():
            messages.info(request,"user name was taken please choose another one")
            return redirect('register')
        else:
            user1=User.objects.create_user(username=username,password=password)
            user1.is_active = False  
         
            messages.success(request,"you account  was created successfully thank you for choosing us ")
            return redirect("sign_in")  
      
    else:
        messages.info(request,"please verify your passwords")
        return redirect('register')   
else:
    return render(request,'register.html')    

The problem with your intial code,

def sign_in(request):
        
    if request.method == "POST":
        username=request.POST['username']
        password=request.POST['password']
        print(username,password)
        user = authenticate(username=username, password=password)
            
        if user is not None:
            login(request,user)
            messages.success(request,"you are logged successfully ")
            return redirect("front_page")    
        else:
            messages.error(request,"please verify your details")
            return redirect("sign_in")   
    return render(request,"login.html")

is that you are trying to authenticate a user that has not signed up yet. This is similar to the code I found in the docs , but that code is to login users, not to sign them up.

The solution, is then:

def sign_in(request):

    if request.method == "POST":
        username=request.POST['username']
        password=request.POST['password']
        user, created = User.objects.get_or_create(username=username, password=password)
        user = authenticate(username=username, password=raw_password)
        login(request, user)
        return redirect("front_page")

    return render(request,"login.html")

Thanks to Sunderam for pointing out my error, the function authenticate() does indeed hash the password, so it will take in a raw password.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM