Django - ImageField files not showing up?

2019-09-15 00:12发布

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']

2条回答
看我几分像从前
2楼-- · 2019-09-15 00:57

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.

查看更多
Anthone
3楼-- · 2019-09-15 01:02

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?

查看更多
登录 后发表回答