您能帮我在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/