Python 如何修复表单验证';未知';在django
当我在基于类的视图中使用单一表单时,没有问题,但当我在基于类的视图中使用多表单时,图像字段的验证失败。我试图用堆栈溢出中提供的上一个解决方案进行修复,但无法解决它 views.py 视图中的代码Python 如何修复表单验证';未知';在django,python,django,django-forms,django-templates,Python,Django,Django Forms,Django Templates,当我在基于类的视图中使用单一表单时,没有问题,但当我在基于类的视图中使用多表单时,图像字段的验证失败。我试图用堆栈溢出中提供的上一个解决方案进行修复,但无法解决它 views.py 视图中的代码 class ProfileEditView(View): profile_class = ProfileForm profile_image_class = ProfileImageForm template_name = 'user/profile_edit.html'
class ProfileEditView(View):
profile_class = ProfileForm
profile_image_class = ProfileImageForm
template_name = 'user/profile_edit.html'
def get(self,request,pk):
if pk:
user = User.objects.get(pk=pk)
profile = Profile.objects.get(user = user)
profile_image = ProfileImage.objects.get(user = user)
profile_form = self.profile_class(instance = profile)
profile_image_form = self.profile_image_class(instance = profile_image)
context = {
'profile_form':profile_form,
'profile_image_form':profile_image_form
}
return render(request, self.template_name, context)
else:
profile_form = self.profile_class(None)
profile_image_form = self.profile_image_class(None)
context = {
'profile_form':profile_form,
'profile_image_form':profile_image_form
}
return render(request, self.template_name, context)
def post(self,request,pk=None, **kwargs):
profile_form = self.profile_class(request.POST,instance=Profile())
profile_image_form = self.profile_image_class(request.POST,instance=ProfileImage())
if profile_image_form.is_valid(): #and profile_image_form.is_valid():
profile = profile_form.save(commit=False)
profile_image = profile_image_form.save(commit=False)
profile.user = self.request.user
profile_image.user = self.request.user
profile.save()
profile_image.save()
return redirect('music:album_list')
context = {
'profile_form':profile_form,
'profile_image_form':profile_image_form,
'error_message':'Something went wrong',
}
return render(request, self.template_name, context)
models.py
模型中的代码
def get_profile_upload_to(instance,filename):
new_filename = '{}.{}'.format(uuid4,filename.split('.')[-1])
return "profile/{}/{}".format(instance.user.id, new_filename)
class ProfileImage(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
image = models.ImageField(upload_to=get_profile_upload_to)
uploaded = models.DateTimeField(auto_now=True)
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
bio = models.TextField(max_length=500,null=True, blank=True)
location = models.CharField(max_length=50,null=True, blank=True)
birth_date = models.DateField(null=True, blank=True)
email_confirmed = models.BooleanField(default=False)
form.py
form.py中的代码
class ProfileImageForm(forms.ModelForm):
class Meta:
model = ProfileImage
fields = ['image']
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['birth_date']
profile_edit.html
html页面中的代码
<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{error_message}}
{{profile_form}}
{{profile_image_form}}
<button type="submit">Submit</button>
</form>
{%csrf\u令牌%}
{{error_message}}
{{profile_form}}
{{profile_image_form}
提交
错误代码
当我打印表单时,我在下面几行看到了。不知道为什么图像字段为false
[21/Oct/2019 08:24:24] "POST /user/profile/46/ HTTP/1.1" 200 2861
profile_form is : True
profile_image_form is : False
profile_form is : <bound method BaseForm.is_valid of <ProfileForm bound=True, valid=True, fields=(birth_date)>>
profile_image_form is : <bound method BaseForm.is_valid of <ProfileImageForm bound=True, valid=False, fields=(image)>>
[21/Oct/2019 08:24:24]“POST/user/profile/46/HTTP/1.1”200 2861
配置文件形式为:True
配置文件\图像\形式为:False
个人资料表格为:
配置文件\图像\格式为:
这里有很多错误。例如,您的profile\u表单
甚至似乎不是表单
但是您的直接错误是没有将文件数据传递到图像表单。应该是:
profile_image_form = self.profile_image_class(request.POST, request.FILES)
(请注意,传递空实例没有意义,只需将其忽略即可。)
还要确保模板中的表单具有正确的类型:
<form action="" method="post" enctype="mulitpart/form-data">
您应该将request.FILES传递给ProfileImageForm,为了获取其中的文件,请确保表单模板中的enctype是
enctype=“multipart/form data”