如何弹出警报-Django模板

发布于 2025-01-30 17:58:02 字数 2570 浏览 2 评论 0 原文

我已经编写了用于用户输入信息的模板,每个用户都将拥有单独的密码。我想在输入密码时验证密码,如果密码不正确,他们会收到弹出消息说“错误的密码”或这样的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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2025-02-06 17:58:02

消息框架使您可以在一个请求中设置消息,并在另一个请求中检索它们。您需要更新模板以检索它们并指定如何

例如,您可以选择使用 bootstrap nerver

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文