【发布时间】:2018-04-13 07:55:07
【问题描述】:
我希望在我的 Django 项目中显示 alert。
我将值从 views.py 传递到 singup.html 以根据条件显示警报。 但最初我在 HTML 中的警报变量没有获得价值。 只有在点击提交按钮后,警报才会被初始化并显示所需的警报。
Views.py
def signup(request):
d = {}
data={}
template={}
context={}
print(request.POST)
if request.method == 'POST':
form = RegisterForm1(request.POST)
print("hello212")
if request.POST.get('email1') and request.POST.get('password1'):
post = Register1()
post.email = request.POST.get('email1')
post.password = request.POST.get('password1')
post.repeatpassword=request.POST.get('repeatpassword1')
print(post.email)
print(post.password)
print(post.repeatpassword)
data=Register.objects.filter(email__iexact=post.email).exists()
print (data)
if post.password == post.repeatpassword:
if data == False:
post.save()
alert = 0
print("check")
else:
alert = 2
context={
"alert":alert,
}
print (context)
# template= "personal_anshul/signup.html"
return render(request, "personal_anshul/signup.html", context)
注册:
<input type="hidden" name="alert" value="{{alert}}" readonly>
<script>
function check() {
<!--{% for message in messages %}-->
<!--<div class="alert alert-{{ message.tags }}">{{ message }}</div>-->
<!--{% endfor %}-->
<!--alert("check")-->
console.log({{ alert }})
if ({{alert}} == 2)
{
alert("id exists")
}
else{
alert("You Genius!")
}
}
</script>
第一次访问我的页面时的警报值: enter image description here
点击提交按钮后的警报值: enter image description here
如何初始化警报,以便在我第一次访问我的页面时获得一个值来比较我的状况?目前警报在点击提交按钮后获得价值。
【问题讨论】:
-
ajax 是您要查找的术语。当用户输入他/她的邮件时,您应该进行 ajax 调用以检查邮件是否已被使用并返回 true 或 false ...您可以直接(在用户输入/填写表单后)发送警报。
标签: javascript django html alert