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.