您能帮我在 Django 表单的视图上上传图片吗?
模型.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)
表格.py
class ProfileForm(forms.ModelForm):
class Meta:
model = User_Profile
exclude = ('user')
视图.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"