使用update()方法时,Django ImageField没有更新 [英] Django ImageField is not updating when update() method is used

查看:418
本文介绍了使用update()方法时,Django ImageField没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从views.py文件更新模型中的某些字段.我使用时,所有其他字段都会正确更新

I am updating some of fields in model from views.py file. All other fields are updated properly when I use

Profile.objects.filter(id=user_profile.id).update(
        bio=bio,
        city=city,
        date_of_birth=dob,
        profile_pic=profile_pic,
        gender=gender
    )

profile_pic = models.ImageField(blank=True)未更新,奇怪的是,当我从admins.py检查我的Profile模型时,它显示了我上载的文件名,但是我的文件未显示在我的/media目录中(我将所有图片上传到哪里)

only, profile_pic = models.ImageField(blank=True) is not updated, Weird thing is when I check my Profile model from admins.py it shows me the file name which I uploaded, But my file is not shown in my /media directory(where I upload my all Images)

views.py

def edit_submit(request):
if request.method == 'POST':
    profile_pic = request.POST.get('profile_pic')
    bio = request.POST.get('bio')
    city = request.POST.get('city')
    dob = request.POST.get('dob')
    gender = request.POST.get('gender')
    user_profile = Profile.objects.get(user=request.user)
    Profile.objects.filter(id=user_profile.id).update(
        bio=bio,
        city=city,
        date_of_birth=dob,
        profile_pic=profile_pic,
        gender=gender
    )
    return HttpResponseRedirect(reverse('profile', args=[user_profile.id]))

这是我在settings.py中管理媒体文件的方式

This is how I manage my media files in settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

我认为ImageField中仅存储文本,并且Image不会上载到/media目录 注意:我正在使用<input type="file" name="profile_pic" class="change_user_img">从模板获取图像

I think only text is stored in ImageField and Image is not uploded to /media directory Note: I am using <input type="file" name="profile_pic" class="change_user_img"> for getting image from template

推荐答案

QuerySet.update()方法不会在模型上调用save(),因此不会执行将图像放入存储区的常规机制.此外,您必须从request.FILES而不是request.POST检索上载的图像.

The QuerySet.update() method doesn't call save() on the model, and so the usual mechanism that places the image into storage is not executed. In addition, you must retrieve the uploaded image from request.FILES not request.POST.

如果您在模型实例上设置属性然后调用save(),而不是使用update(),则应将映像保存到磁盘上的正确位置.例如:

Rather than using update(), if you set the attributes on the model instance and then call save(), the image should get saved to the correct place on disk. For example:

profile_pic = request.FILES.get('profile_pic')  # Use request.FILES

bio = request.POST.get('bio')
city = request.POST.get('city')
dob = request.POST.get('dob')
gender = request.POST.get('gender')

user_profile = Profile.objects.get(user=request.user)
user_profile.bio = bio
user_profile.city = city
user_profile.date_of_birth = dob
user_profile.profile_pic = profile_pic
user_profile.gender = gender
user_profile.save()

如评论中所述,还必须确保您的表单已设置enctype="multipart/form-data".

As mentioned in the comments, you must also ensure that the your form has enctype="multipart/form-data" set.

这篇关于使用update()方法时,Django ImageField没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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