django 最后一个标记对象不可订阅
django last tag object is not subscriptable
我很抱歉问但是...
我一直在尝试使用我的 Django 框架在我的 HTML 中使用这个 "last" 标签,以便在我的 html 上显示我数据库中的最后一个对象;这里文档:https://docs.djangoproject.com/en/2.1/ref/templates/builtins/
现在的问题是当我在我的代码中使用我的 "last" 标签时;
<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>
并得到这个错误;
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
我不清楚这个错误,但即使在阅读这里之后:
In Python, what does it mean if an object is subscriptable or not?
这是我的模型代码;
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
这里是我的视图代码;
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')
不好意思问
您可以测试 for 循环是否引用了带有 forloop.last
的最后一项,然后查找 PH
属性:
<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>
似乎尝试在 tank
s 的查询集上使用 Django 的 last
模板标记会给出 "Negative indexing is not supported" 错误。
我很抱歉问但是...
我一直在尝试使用我的 Django 框架在我的 HTML 中使用这个 "last" 标签,以便在我的 html 上显示我数据库中的最后一个对象;这里文档:https://docs.djangoproject.com/en/2.1/ref/templates/builtins/
现在的问题是当我在我的代码中使用我的 "last" 标签时;
<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>
并得到这个错误;
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
我不清楚这个错误,但即使在阅读这里之后: In Python, what does it mean if an object is subscriptable or not?
这是我的模型代码;
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
这里是我的视图代码;
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')
不好意思问
您可以测试 for 循环是否引用了带有 forloop.last
的最后一项,然后查找 PH
属性:
<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>
似乎尝试在 tank
s 的查询集上使用 Django 的 last
模板标记会给出 "Negative indexing is not supported" 错误。