1

here is my models.py file

class ProductPage(models.Model):

    item=models.CharField(max_length=100)
    price=models.IntegerField(null=True, blank=True)

    @property
    def price_tag(self):
      if price>=100000 or price<=9999999:
        price=price//100000
        return self.str(price)+ 'lac'

my view.py file.

def product(request):

object1=ProductPage.objects.all()
return render(request,'item.html',{'object1':object1}) 

and index.html file:

{% for i in object1 %}

           <tr> 
             <td>{{i.id}}</td>
             <td>{{i.item}}</td>
             <td>{{i.price_tag}}</td>
       {% endfor%}

In my admin panel price is in form of integer field i.e 10000. I want to convert it into a form as '1 lac' at the display page (see Indian Numbering System). How to do that?

James Z
  • 12,209
  • 10
  • 24
  • 44
Amit Saini
  • 136
  • 2
  • 16
  • `if price>=100000 or price<=9999999` is true for any price below 10000000. Probably not what you mean. But what is it showing now? –  Oct 23 '20 at 08:32
  • if price>=100000 or price<=9999999 this line give me an error local variable 'price' referenced before assignment – Amit Saini Oct 23 '20 at 08:39

1 Answers1

1

Recommended Reading, Recommended Reading 2

Recommended Before doing more Django

Your variable price will be resolved to the field declaration on the model. This isn't how things work in OOP and Django specifically. There's 4 mistakes in total, all corrected below. Try to figure out why:

class Product(models.Model):

    item=models.CharField(max_length=100)
    price=models.IntegerField(null=True, blank=True)

    @property
    def price_tag(self):
      if self.price>=100000 and price<=9999999:
        price=price//100000
        return f"{self.price}lac"
      return str(self.price) if self.price is not None else ""