[Django]-TemplateSyntaxError while uploading image using Django Admin

6👍

Just change your unicode function of Image class

class Image(models.Model):
    celebrity = models.ForeignKey(Celebrity)
    image = models.ImageField('Bild', upload_to="files/")

    def __unicode__(self):
       return self.image.name

You an also refer to this discussion for reference.I am quoting the reason of this error:

The problem with this issue finally had nothing to do with the forms
themselves, but the unicode method of the Image-model.
Older implementations of ImageField had a unicode method of their own so

def __unicode__(self):
     return self.file

worked fine. Now this needs to be

def __unicode__(self):
    return self.file.name

to achieve the same result.

2👍

The traceback tells you exactly what is going on. The error is in the __unicode__ method of the Image class – you’re returning the image property there, but that’s not a string, so can’t be output as unicode.

Try returning image.url instead.

Leave a comment