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 >
<form action="{% url 'sign_in' %}" method="POST">
{% csrf_token %}
<h2 >Login</h2>
<p >Please fill in this form to login your account!</p>
<hr>
<div >
<div >
<div >
<span >
<span ></span>
</span>
</div>
<input type="text" name="username" placeholder="Username" required="required">
</div>
</div>
<div >
<div >
<div >
<span >
<i ></i>
</span>
</div>
<input type="password" name="password" placeholder="Password" required="required">
</div>
</div>
<div >
<button type="submit" >Login</button>
</div>
<div >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?
CodePudding user response:
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')
CodePudding user response:
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.