[Django]-Django The 'image' attribute has no file associated with it

124๐Ÿ‘

โœ…

bob and person are the same object,

person = Person.objects.get(user=request.user)
bob = Person.objects.get(user=request.user)

So you can use just person for it.

In your template, check image exist or not first,

{% if person.image %}
    <img src="{{ person.image.url }}">
{% endif %}
๐Ÿ‘คiMom0

96๐Ÿ‘

The better approach which would not violate DRY is to add a helper method to the model class like:

@property
def image_url(self):
    if self.image and hasattr(self.image, 'url'):
        return self.image.url

and use default_if_none template filter to provide default url:

<img src="{{ object.image_url|default_if_none:'#' }}" />
๐Ÿ‘คtejinderss

15๐Ÿ‘

My dear friend, others solvings are good but not enough because If user hasnโ€™t profile picture you should show default image easily (not need migration). So you can follow below steps:

Add this method to your person model:

@property
def get_photo_url(self):
    if self.photo and hasattr(self.photo, 'url'):
        return self.photo.url
    else:
        return "/static/images/user.jpg"

You can use any path (/media, /static etc.) but donโ€™t forget putting default user photo as user.jpg to your path.

And change your code in template like below:

<img src="{{ profile.get_photo_url }}" class="img-responsive thumbnail " alt="img">
๐Ÿ‘คBurak Ibis

6๐Ÿ‘

Not exactly what OP was looking for, but another possible solution would be to set a default value for ImageField:

 class Profile(models.Model):
    # rest of the fields here
    image = models.ImageField(
        upload_to='profile_pics/',
        default='profile_pics/default.jpg')

5๐Ÿ‘

you have two choices :

  • first one:

in the model field try to put a default image value , like this :

PRF_image = models.ImageField(upload_to='profile_img', blank=True, null=True , default='profile_img/925667.jpg')
  • the second one (recommended) :

add a custom method inside your class model like the following , to return PRF_image url if exist or return empty string if not :

PRF_image = models.ImageField(upload_to='profile_img', blank=True, null=True )

@property
def my_PRF_image(self):

    if self.PRF_image :
        return self.PRF_image.url
    return ''

and inside your template you can use :

{{ your_object.my_PRF_image }}

i hope this helpful .

๐Ÿ‘คK.A

3๐Ÿ‘

You can also use the Python 3 built-in function getattr to create your new property:

@property
def image_url(self):
    """
    Return self.photo.url if self.photo is not None, 
    'url' exist and has a value, else, return None.
    """
    if self.image:
        return getattr(self.photo, 'url', None)
    return None

and use this property in your template:

<img src="{{ my_obj.image_url|default_if_none:'#' }}" />
๐Ÿ‘คMartin

2๐Ÿ‘

Many way to solve this issue

Try below code

models.py # under Person class

@property
def imageURL(self):
    if self.image:
        return self.image.url
    else:
        return 'images/placeholder.png'

html file

<img src="{% static person.imageURL %}" class="thumbnail" />
๐Ÿ‘คAkash Darji

1๐Ÿ‘

Maybe this helps but my database didnโ€™t save on of the pictures for the object displayed on the page.

As that object in models.py has blank=False and also I am looping through object, it constantly gave an error until I added a replacement picture in the admin for the database to render.

๐Ÿ‘คKlim Yadrintsev

0๐Ÿ‘

This error also arises when any one or more items doesnโ€™t have an image added and the rest items do. To fix this:

class Product(models.Model):
    pname = models.CharField(max_length=30)
    price = models.IntegerField()
    img = models.ImageField(null = True,blank = True)
    def __str__(self):
        return self.pname
    @property
    def imageURL(self):
        try:
            url = self.img.url
        except:
            url=''
        return url
๐Ÿ‘คPooja

0๐Ÿ‘

I had a similar problem , but my issue was with the form in HTML template. if you donโ€™t set the enctype = "multipart/form-data" attribute then it does not upload the image hence the reason for the error

๐Ÿ‘คtimothy murega

Leave a comment