如何弹出警报-Django模板
我已经编写了用于用户输入信息的模板,每个用户都将拥有单独的密码。我想在输入密码时验证密码,如果密码不正确,他们会收到弹出消息说“错误的密码”或这样的sth。
我试图导入消息库,但是它无法正常工作,您能提供帮助吗?
以下是我的代码:
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')
模板/帖子/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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
消息框架使您可以在一个请求中设置消息,并在另一个请求中检索它们。您需要更新模板以检索它们并指定如何。
例如,您可以选择使用 bootstrap nerver :
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: