如何弹出警报 - Django 模板
How to popup the alert - Django template
我写了用户输入信息的模板,每个用户都会有单独的密码。我想在他们输入时验证密码,如果密码不正确,他们会收到弹出消息说“密码错误..”或类似的东西。
我试图导入消息库,但是它不能正常工作,你能帮忙吗?
下面是我的代码:
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/bootstrap@5.1.3/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>
消息框架允许您在一个请求中设置消息,并在另一个请求中检索它们。您需要更新模板以检索它们并指定如何 display them.
例如,您可以选择使用 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>
我写了用户输入信息的模板,每个用户都会有单独的密码。我想在他们输入时验证密码,如果密码不正确,他们会收到弹出消息说“密码错误..”或类似的东西。
我试图导入消息库,但是它不能正常工作,你能帮忙吗?
下面是我的代码:
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/bootstrap@5.1.3/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>
消息框架允许您在一个请求中设置消息,并在另一个请求中检索它们。您需要更新模板以检索它们并指定如何 display them.
例如,您可以选择使用 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>