简体   繁体   English

用户未在 Django 中进行身份验证? 它显示错误

[英]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视图.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网址.py

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

html 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: authenticate()还需要request作为参数,因此它应该是:

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.感谢Sunderam指出我的错误,function authenticate()确实是hash 密码,所以它会接受原始密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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