How do I get an error message from an object

Viewed 27

I'm trying to create an authentication in my project after signing in. I created my models and form. 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 create the pin in my models already

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

  <style>
        h5{
          color: red;
        }
      </style>
      {% for message in messages %}
      <h5> {{message}}</h5>
      {% endfor %}
      <form action="portfolio" method="POST" class="row g-3"></form>
      {% csrf_token %}
      
           
        <div class="col-md-12">
            <label for="validationDefault02" class="form-label"></label>
            <input type="text" name = 'pin' class="form-control"    id="validationDefault02" value="" required>
          </div>
        
          <div class="col-12">
            <button class="btn btn-primary" type="submit">Send</button>
          </div>
        </form>

and this is my views

@login_required(login_url = 'loginss')
def account(request):
    form = PortfolioForm()
    if request.method == 'POST':
            try:
                pin = Portfolio.objects.get(foo='bar')
            except Portfolio.DoesNotExist:
                pin = None
    context = {'form' : form}
    return render(request, 'account.html', context)
1 Answers

Because you aren't using a Form.py, you'd do something like this:

def account(request):
    form = PortfolioForm()
    context = {'form' : form}
    if request.method == 'POST':
        try:
            pin = Portfolio.objects.get(foo='bar')
        except Portfolio.DoesNotExist:
            pin = None
            context['error'] = 'Pin Not Found
    return render(request, 'account.html', context)
<form action="portfolio" method="POST" class="row g-3"></form>
    {% csrf_token %}


    <div class="col-md-12">
        <label for="validationDefault02" class="form-label"></label>
        <input type="text" name = 'pin' class="form-control"    id="validationDefault02" value="" required>
        {% if error %}
            <span style="color:red">{{error}}</span>
        {% endif %}
    </div>

    <div class="col-12">
        <button class="btn btn-primary" type="submit">Send</button>
    </div>
</form>

But instead of doing a try/except, just do a .filter().first()

  1. It'll be None if it's not found and you can use that in an If
  2. No potential actual errors
def account(request):
    form = PortfolioForm()
    context = {'form' : form}
    if request.method == 'POST':
        pin = Portfolio.objects.filter(foo='bar').first()
        if not pin:
            context['error'] = 'Pin Not Found
    return render(request, 'account.html', context)
Related