Home > front end >  How do i get a success message for a model object in django
How do i get a success message for a model object in django

Time:09-19

I'm trying to create an authentication in my project after signing in. I created my models. I want the user to get an error message if he doesn't get the correct pin in the models and successful if he puts in the right pin. I only get the error message but i don't get the success message

class Data(models.Model):
name = models.CharField(max_length=100)
body = models.CharField(max_length=1000000)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)  
other_name = models.CharField(max_length=200)
email = models.EmailField()
profile_image = models.ImageField(blank=True)
city = models.CharField(max_length= 100)
title = models.TextField(null=True, blank=True)
middlename = models.TextField(null=True, blank=True)
adreess = models.TextField(null=True, blank=True)
country = models.TextField(null=True, blank=True)
state = models.TextField(blank=True, verbose_name="Biography (Age, Language, Location...)")
pin = models.IntegerField()
available = models.TextField(null=True, blank=True)
checkings = models.TextField(null=True, blank=True)
savings = models.TextField(null=True, blank=True)
phone_number = models.TextField(null=True, blank=True)
first_his = models.TextField(null=True, blank=True)
second_history =  models.TextField(null=True, blank=True)
third_history = models.TextField(null=True, blank=True)
last_history = models.TextField(null=True, blank=True)
newstate = models.TextField(null=True, blank=True)


def __str__(self):
    return self.first_name 

This is my view

def checkview(request):
pin = request.POST['pin']


if Data.objects.filter(name=pin).exists():
    messages.success(request, "You have registered successfully")
    
    
else:
   messages.error(request, 'Account Suspended, Your Request To Transfer funds Has Been Declined. Contact Support for Help') 
   return redirect('account')

This is my Htm file

<div >
    <h1>Pin Code Required to Complete Transfer Request</h1>
    <style>
      h8{
        color: red;
      }
    </style>
    {% for message in messages %}
    <h8> {{message}}</h8>
    {% endfor %}
    <form action = "checkview"  method="POST" autocomplete="on">
      {% csrf_token %}
    <!--Second name-->
    <div >
      <label for="pin" > </label>
            <div ><i  aria-hidden="true"></i></div>
            <div >
                    <input type="text" name="pin" maxlength="4" 
           >
            </div>
            <div ></div>
            {% if error %}
                <span style="color:red">{{error}}</span>
            {% endif %}
        </div>
        <!--Second name-->

       <!---Submit Button------>
                      
       <input type="Submit" value="Transfer">
                      
       <!---Submit Button----->

    </form>

CodePudding user response:

I think, you returns nothing on success. in your case:

def checkview(request):
    pin = request.POST.get('pin') or ''    
    if Data.objects.filter(name=pin).exists():
        messages.success(request, "You have registered successfully")
    else:
       messages.error(request, 'Account Suspended, Your Request To Transfer funds Has Been Declined. Contact Support for Help') 
   
return redirect('account')

CodePudding user response:

I think there's a typo in line:

if Data.objects.filter(name=pin).exists():

which should be 'pin=pin' and not 'name=pin'

if Data.objects.filter(pin=pin).exists():
  • Related