django - 上载图像文件

标签 django file-upload image-upload

您能帮我在Django表单的 View 上进行图像上传吗?

型号

class User_Profile(models.Model):
    user = models.OneToOneField(User, unique=True, related_name='profile')
    photo  = models.ImageField(upload_to = 'profiles/', null=True, blank=True)

Forms.py
class ProfileForm(forms.ModelForm):
        class Meta:
            model = User_Profile
            exclude = ('user')

Views.py
    if request.method == 'POST':
        profile_form = ProfileForm(request.POST, instance=request.user.profile)

        if profile_form.is_valid():
            profile_form.save()
            return HttpResponseRedirect('/panel/correct/')

    else:
        profile_form = ProfileForm(instance=request.user.profile)

我的HTML表单已经包含enctype="multipart/form-data"

最佳答案

您似乎不是binding the file data to the form

profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) 

关于django - 上载图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1078786/

相关文章:

python - 连接到 Django 查询中的新字段

python - Django - 不是注册的命名空间

javascript - JavaScript 的 Django 反向()

java - 公共(public)文件上传(Apache)

django - unique_together 在 Django-nonrel 中如何工作?

javascript - Chrome 不支持 TypedArray.prototype.slice()

javascript - 输入文件上传多个文件在移动设备上不起作用

javascript - 两次上传相同的图像文件 - Javascript

php - 将带有缩略图的图像上传到文件夹并添加到 Mysql 的路径

android - 如何同时在 amazon web service s3 (Android) 中上传多张图片?