[Answered ]-Django – ImageField files not showing up?

1👍

Your problem is here:

>>> b = UserImages(user=a)
>>> b
<UserImages: UserImages object>
>>> b.photo
<ImageFieldFile: None>

This code snippet is creating a new instance of UserImages, setting the user attribute to the object a. It is not searching the database. Since you haven’t attached any images to this new instance, b is None.

To search, you need to do this instead:

>>> b = UserImages.objects.filter(user=a)

You also shouldn’t upload anything to the same folder that is pointed to by STATICFILES_DIRS, as this folder is managed by django and your files here will be overwritten. I hope /home/images/static isn’t listed here.

User uploads are saved in a subdirectory pointed to by MEDIA_FILES and accessed using MEDIA_URL.

1👍

When you save images in the database they will be saved in there as well as in a new location in your /static/ directory, etc.. Usually Django attaches a image_1.jpg for example if the image was originally image.jpg.

Do your images have a many-to-many relationship to the User model? Earlier, you said that there were 4 images saved to the User, then you said 1. Your UserImages model has one field, so possibly you are not looping through it correctly in the terminal shell in order to check all images. Perhaps it needs to be b.photos.all() if b = UserImages(user=a) or something to that extent?

Leave a comment