1

I apologies to ask but...

I been trying on this "last" tags in my HTML with my Django framework to bring out the last object in my database to be display on my html; here doc:https://docs.djangoproject.com/en/2.1/ref/templates/builtins/

Now the problem was when I use my "last" tags on my code;

<div class = "float-right  my-4 chartjs-render-monitor" id="chartContainerPH" style="width: 49%; height: 400px;display: inline-block; background-color:#FDFDFD;">
<center>
    <a class="title-link" href="{%url 'ph' %}">PH:</a>
    <p> {% for tank_system in tank %}{{tank_system.PH|last}}, {%endfor%}</p>
    </center>

</div>

and get this errors;

TypeError at /
'decimal.Decimal' object is not subscriptable
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 2.1.3
Exception Type: TypeError
Exception Value:    
'decimal.Decimal' object is not subscriptable
Exception Location: C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaultfilters.py in last, line 540
Python Executable:  C:\Users\user\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version: 3.7.1
Python Path:    
['C:\\Users\\user\\Desktop\\FrounterWeb- postgreDB',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
Server time:    Thu, 6 Dec 2018 11:12:47 +0800

I'm not clear on this errors but even after read here: In Python, what does it mean if an object is subscriptable or not?

here's my code on models;

from django.db import models
from django.utils import timezone
# having errors KeyError: "'__name__' not in globals"

class tank_system(models.Model):
    PH = models.DecimalField(max_digits=3, decimal_places=1)
    EC = models.DecimalField(max_digits=3, decimal_places=1)
    WaterLevel = models.IntegerField(default=100)
    TempWater = models.IntegerField(default=0)
    TempRoom = models.IntegerField(default=0)
    datetime = models.DateTimeField(default=timezone.now())



    def get_last(self):
        try:
            print(self)
            return self.set.all().order_by('-date')[0]
        except IndexError:
            pass

here my code on views;

from django.shortcuts import render
from django.views.generic import TemplateView
from zigview.models import tank_system

from django.contrib.auth.decorators import login_required
import logging
logger = logging.getLogger(__name__)
from django.core.mail import send_mail



try:
    @login_required(login_url='/accounts/login/')
    def index(request): #gose to main dashboard page
        #tank = tank_system.objects.all()
        tank = tank_system.objects.extra(select={'is_recent':"datetime>'2006-01-01'"})
        return render(request,'FrounterWeb/extends/includes.html',{'tank':tank})
except:
    logger.error('index page request failed/errors')

I apologies to ask

Rookies DJ
  • 542
  • 1
  • 5
  • 18
  • 1
    It's unclear what you're trying to do in template. You've got a list of `tank`s. And trying to apply `last` filter to `decimal` value like 99.99. Last filter wont work in this case, it usually applied to queryset or list. Please clarify, do you want to show `PH` only for the last `tank_system` or ...? – Satevg Dec 06 '18 at 07:57
  • I'm trying to display the last element in my list and I need to be able to multiple my list to be precise – Rookies DJ Dec 07 '18 at 04:00

1 Answers1

4

You can test whether the for loop is referencing the last item with forloop.last, and then look up the PH attribute on that:

<center>
    <a class="title-link" href="{%url 'ph' %}">PH:</a>
    <p> 
       {% for tank_system in tank %}
           {% if forloop.last %}
               {{ tank_system.PH }}, 
           {% endif %}
       {% endfor %}
    </p>
</center>

It seems that trying to use Django's last template tag on the queryset of tanks will give the "Negative indexing is not supported" error.

Will Keeling
  • 22,055
  • 4
  • 51
  • 61
  • It didn't work, it switches to this error; AssertionError at / Negative indexing is not supported. I guessing these errors mean there a negative in the Query set. or the "last" require a negative? – Rookies DJ Dec 09 '18 at 08:46
  • 2
    @RookiesDJ it turns out that the `last` template tag will give the "Negative indexing is not supported" error. I have updated the answer with an alternative solution. – Will Keeling Dec 09 '18 at 09:16
  • 1
    Thank you will keeling, it work. Is there way I can repaid your help. your life savoir. – Rookies DJ Dec 10 '18 at 03:12
  • still a bit don't understand. how the work code is there source code i can read form? and thank you so much on for your help. How can I ever repaid you – Rookies DJ Dec 10 '18 at 03:15
  • 2
    @RookiesDJ glad to help. I wasn't completely sure what you meant by "how the work code is there source code i can read from" but you can find more in the docs regarding Django's [`forloop.last`](https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#for). Perhaps you could accept the answer if things are working ok now?. Thanks! – Will Keeling Dec 10 '18 at 08:32
  • I mean is their page where I can read how code runs, but never mind about it. Will thank you – Rookies DJ Dec 10 '18 at 08:42