Presenting percents of Model values in simple templates?

165 views
Skip to first unread message

JLeary

unread,
Jun 10, 2015, 7:11:35 AM6/10/15
to django...@googlegroups.com
Hi.
Total Django Newbie here... and pretty new to Python too.
Anyway, I have this simple problem, and I've been reading documentation all afternoon but just can't come up with a simple solution.

How can I display calculated values from the Model vales store in the database?
Like, a percentage of two numbers?

For instance;

# myapp/models.py
class Container(models.Model):
    name
= models.CharField(max_length=128)
    capacity
= models.IntegerField()
    contains
= models.IntegerField()
   
   
python manage
.py shell
>>> Container(name="big bucket", capacity=100, contains=22).save()
>>> Container(name="small bucket", capacity=50, contains=48).save()
>>> Container(name="medium bucket", capacity=75, contains=13).save()


# myapp/views.py
from django.http import HttpResponse
from django.template import RequestContext, loader
from .models import Container
def index(request):
    object_list
= Container.objects.all()
   
template = loader.get_template('myapp/index.html')
    context
= RequestContext(request, {
       
'object_list': object_list,
   
})
   
return HttpResponse(template.render(context))

   
# myapp/templates/myapp/index.html
{% if object_list %}
   
<table>
   
{% for o in object_list %}
       
<tr>
       
<td>{{ o.name }}</td> <td>{{ o.capacity }}</td> <td>{{ o.contains }}</td>
       
</tr>
    {% endfor %}
    </
table>
{% endif %}

.


So, all is fine when I create a simple view template to display the NAME, CAPACITY, and CONTAINS info for each object.

What I really want... is the Percent-Full of each Container to be displayed.
But, I'm stumped as to how I can do this.

I've tried creating a sub-class of Container, that contained methods to return the percentages, but was unable to get it to work.

Any input is appreciated.
Thanks.

Vijay Khemlani

unread,
Jun 10, 2015, 8:29:01 AM6/10/15
to django...@googlegroups.com
You can add methods to your model class

class Container(models.Model):
    name = models.CharField(max_length=128)
    capacity = models.IntegerField()
    contains = models.IntegerField()

    def usage_percentage():
        return 100.0 * self.contains / self.capacity

And in your template

{% if object_list %}
    <table>
    {% for o in object_list %}
        <tr>
        <td>{{ o.name }}</td> <td>{{ o.capacity }}</td> <td>{{ o.contains }}</td> <td> {{ o.usage_percentage }} </td>
        </tr>
    {% endfor %}
    </table>
{% endif %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/386ecbdd-79e1-4646-b444-c5cd559ceb24%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages