Django - ImageField files not showing up?

狂风中的少年 提交于 2019-12-08 06:08:53

问题


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

回答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.




回答2:


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?



来源:https://stackoverflow.com/questions/22030128/django-imagefield-files-not-showing-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!