题
能不能请你帮我把图像传上工作的观django形式?
Models.py
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"
解决方案
您似乎没有被绑定文件数据到表单。
profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
不隶属于 StackOverflow