Home > Mobile >  How to popup the alert - Django template
How to popup the alert - Django template

Time:05-20

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 >

<body>
<div >
    <!-- <h2>Input your password to continue proceed</h2>
    password: <input id = "pwd" type="password" name="password"/><br/>


    <div > -->
        <!-- <button  onclick="validate()">proceed</button>
    </div> -->
</div>
<div >
    <form action="" method="POST">
        {% csrf_token %}
        <h2>Input your password to continue proceed</h2>
        password: <input id = "pwd" type="password" name="password"/><br/>    
        <div >
        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>

CodePudding user response:

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 >
    {% for message in messages %}
    <div  role="alert">{{ message }}</div>
    {% endfor %}
    <form action="" method="POST">
        {% csrf_token %}
        ...
    </form>
</div>
  • Related