This is my models.py:
class UserImages(models.Model):
user = models.ForeignKey(User)
photo = models.ImageField(upload_to=get_file_path)
and this is my view which uploads images:
def uploadImageView(request):
if request.method == 'POST':
form = UploadImageForm(request.POST, request.FILES)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return redirect('/')
else:
form = UploadImageForm()
return render(request, 'image.html', {'form': form})
and this is my view which displays user's images:
def displayImageView(request):
user = request.user
img = UserImages.objects.filter(Q(user__username=user))
return render(request, "displayImage.html", {"img": img})
and this is my displayImage.html template:
{% load staticfiles %}
{% for image in img %}
<img src="{% static image.photo.url %}" alt="" />
{% endfor %}
and when I go to the URL which loads the template, it displays four images. I remember that while testing the uploadImageView, I uploaded 4 images for the viewer. The location Django saves the images was
/home/images/static
and I went to that folder and deleted two images. I then refreshed the template page and it still displayed 4 images rather than two, so then I figured that I had to delete it from the actual database rather than the folder. I then did
python manage.py shell
>>> from app.models import UserImages
>>> from django.contrib.auth.models import User
>>> a = User.objects.get(username='testUser')
>>> b = UserImages(user=a)
>>> b
<UserImages: UserImages object>
>>> b.photo
<ImageFieldFile: None>
amd as you can see, only one ImageFieldFile shows up for the user who I uploaded 4 images for. How come I can only see one?
EDIT:
my UploadImageForm() is just this:
class UploadImageForm(forms.ModelForm):
class Meta:
model = UserImages
fields = ['photo']
Your problem is here:
This code snippet is creating a new instance of
UserImages
, setting theuser
attribute to the objecta
. It is not searching the database. Since you haven't attached any images to this new instance,b
isNone
.To search, you need to do this instead:
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 usingMEDIA_URL
.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 aimage_1.jpg
for example if the image was originallyimage.jpg
.Do your images have a
many-to-many
relationship to theUser
model? Earlier, you said that there were 4 images saved to theUser
, then you said 1. YourUserImages
model has one field, so possibly you are not looping through it correctly in theterminal shell
in order to check all images. Perhaps it needs to beb.photos.all()
ifb = UserImages(user=a)
or something to that extent?