Issue
I have wrote the template for user input the info , each user will have the separate password. I would like to validate the password when they input , if the password is incorrect they will got the popup message said "wrong password .." or sth like that.
I tried to import messages library, however it doesn't work correctly, could you please help assist ?
below is my code:
views.py
from .models import Register1
@csrf_exempt
def reg(request):
if request.method == 'POST':
if request.POST.get('password') =="Password":
print(request.POST.get('password'))
if request.POST.get('phone') and request.POST.get('name') and request.POST.get('email'):
post=Register1()
post.phone= request.POST.get('phone')
post.name= request.POST.get('name')
post.email= request.POST.get('email')
post.save()
return render(request, 'posts/reg.html')
else:
print(request.POST.get('password'))
messages.add_message(request, messages.INFO, 'Wrong Password')
return render(request,'posts/reg.html')
else:
return render(request,'posts/reg.html')
templates/posts/reg.html
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="container mt-3">
<body>
<div class="col-9">
<!-- <h2>Input your password to continue proceed</h2>
password: <input id = "pwd" type="password" name="password"/><br/>
<div class="col-3 "> -->
<!-- <button class="btn btn-primary" onclick="validate()">proceed</button>
</div> -->
</div>
<div class="col-6 mt-3">
<form action="" method="POST">
{% csrf_token %}
<h2>Input your password to continue proceed</h2>
password: <input id = "pwd" type="password" name="password"/><br/>
<div class="col-3 ">
Phone: <input type="text" name="phone"/><br/>
<!-- Name: <br/>
<textarea cols="35" rows="8" name="name">
</textarea><br/> -->
Name: <input type="text" name="name"/><br/>
Email: <input type="text" name="email"/><br/>
<input type="submit" value="Post"/>
</form>
</div>
</body>
</div>
Solution
The messages framework allows you to set messages in one request, and retrieve them in another. You need to update your template to retrieve them and specify how to display them.
As an example, you could choose to use a Bootstrap Alert:
<div class="col-6 mt-3">
{% for message in messages %}
<div class="alert alert-info" role="alert">{{ message }}</div>
{% endfor %}
<form action="" method="POST">
{% csrf_token %}
...
</form>
</div>
Answered By - MattRowbum
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.