Django - ImageField文件不显示? [英] Django - ImageField files not showing up?

查看:1027
本文介绍了Django - ImageField文件不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的models.py:

  class UserImages(models.Model):
user = models。 ForeignKey(User)
photo = models.ImageField(upload_to = get_file_path)

我上传图片的视图:

  def uploadImageView(request):
if request.method =='POST'
form = UploadImageForm(request.POST,request.FILES)
如果form.is_valid():
instance = form.save(commit = False)
instance.user =请求.user
instance.save()
返回重定向('/')
else:
form = UploadImageForm()

返回渲染(请求, 'image.html',{'form':form})

这是我的视图,显示用户的图像:

  def displayImageView(request):
user = request.user
img = UserImages。 objects.filter(Q(user__username = user))
return render(request,displayImage.html,{img:img})

这是我的displayImage.html模板:

  {%load staticfiles%} 
{%for img%
< img src ={%static image.photo.url%}alt =/>
{%endfor%}

当我转到加载模板的网址时,它显示四个图像。我记得在测试uploadImageView时,我为浏览器上传了4张图像。 Django保存图像的位置是

  / home / images / static 
pre>

,我去了那个文件夹并删除了两个图像。然后我刷新了模板页面,它仍然显示4个图像而不是两个图像,所以我想我必须从实际的数据库而不是文件夹删除它。然后我做了

  python manage.py shell 
>>>来自app.models import UserImages
>>>来自django.contrib.auth.models import User
>>> a = User.objects.get(username ='testUser')
>>> b = UserImages(user = a)
>>> b
< UserImages:UserImages对象>
>>> b.photo
< ImageFieldFile:None>

amd,您可以看到,只有一个ImageFieldFile显示给我上传了4张图片的用户。如何才能看到一个?



编辑:



我的UploadImageForm()只是这样: p>

  class UploadImageForm(forms.ModelForm):
class Meta:
model = UserImages
fields = ['photo']


解决方案

你的问题在这里: / p>

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

此代码段正在创建一个新的实例 UserImages ,将用户属性设置为对象 a 。它不是搜索数据库。由于您尚未将任何图像附加到此新实例,因此 b



要进行搜索,您需要执行此操作:

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

你也不应该上传任何东西该文件夹由 STATICFILES_DIRS 指向的文件夹,因为此文件夹由django管理,您的文件将被覆盖。我希望 / home / images / static 不在这里列出。



用户上传保存在指向的子目录中到$ code> MEDIA_FILES 并使用 MEDIA_URL 访问。


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:

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

这篇关于Django - ImageField文件不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆