用户在Django中未通过身份验证?显示错误

knpiaxh1  于 2022-11-18  发布在  Go
关注(0)|答案(2)|浏览(175)

我正在创建一个网站,所以我完成了登录页面中的注册页,每个细节都是正确的,但这是显示请验证您的详细信息,我的意思是其他部分也我把在用户名和密码后打印语句相同的详细信息是打印时,我键入,但不访问登录。
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"),

超文本标记语言

<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>

我只想用用户名和密码登录,但它显示请验证您的详细信息,但所有的详细信息都是正确的。
有什么想法吗?

rryofs0p

rryofs0p1#

authenticate()也需要request做为参数,因此它应该是:

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

注册用户的视图应如下所示:

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')
s2j5cfk0

s2j5cfk02#

你的初始代码有问题

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 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")

感谢Sunderam指出了我的错误,函数authenticate()* 确实 * 对密码进行了哈希处理,因此它将接受一个原始密码。

相关问题