I have a problem. What the program does is upload images and display them in an index.html, in local it works quite well. Upload the image and present it in the index. I don't know why when I transfer my program to Digital Ocean I get this error:
IndexError at /carros/
list index out of range
/workspace/carros/models.py, line 33, in new_name
The error is recorded right where @property new_name I don't know anything about Json, in fact I'm touching a colleague's code to upload it to Digital Ocean but I find it curious that it only works locally. Do you have any idea what is going on?
models.py
from datetime import timezone, datetime
from django.db import models
# Create your models here.
from Clientes.models import Clientes
import json
class Carro(models.Model):
placas=models.CharField(max_length=255, blank=True)
tipo=models.CharField(max_length=255, blank=True)
marca=models.CharField(max_length=255, blank=True)
modelo=models.CharField(max_length=255, blank=True)
año=models.IntegerField()
vin=models.CharField(max_length=255, blank=True)
color=models.CharField(max_length=255, blank=True)
motor=models.CharField(max_length=255, blank=True)
agente_seguros=models.CharField(max_length=255, blank=True)
compañia_seguros=models.CharField(max_length=255, blank=True)
no_politica=models.CharField(max_length=255,blank=True)
cliente= models.ForeignKey(Clientes, on_delete=models.SET_NULL, null=True,blank=True)
fotosCarro=models.TextField(null=True,blank=True)
garantia=models.TextField(null=True,blank=True)
fecha_registros = models.DateTimeField(default=datetime.now, null=True,blank=True)
#customize model property for main view of fotosCarro
@property
def new_name(self):
temp=json.loads(self.fotosCarro)
tempkey=sorted(temp.keys())
for item in reversed(tempkey):
if temp[item][1]=="on":
return item;
return list(json.loads(self.fotosCarro).keys())[0]
def orig_name(self):
return list(json.loads(self.fotosCarro).values())[0]
def __str__(self):
return f'{self.placas} {self.año}{self.marca} {self.modelo} {self.tipo}{self.motor}{self.vin}{self.color}' \
f'{self.agente_seguros}{self.compañia_seguros}{self.no_politica}{self.cliente}{self.fotosCarro}{self.garantia}' \
f'{self.fecha_registros}'
To avoid this kind of errors, you have to verify that your list is not empty, the error is thrown by one of the two properties, the one of new_name or the one of orig_name, you need to validate before that the keys() and values() exist before to convert it to list and try to get the first element of the list.
Here is a way how you can implement it: