Ad

How To Fix Form Validation 'unknow' In Django

When I use single form there is no problem but when I used multi-forms in a class based view it's getting validation failed with image field. I tried to fix with previous solution provided in stack overflow but couldn't able to solve it.

views.py

codes in views

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

codes in model

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

codes in 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

code in html page.

<form method="post" enctype="multipart/form-data">{% csrf_token %}
    {{error_message}}

    {{profile_form}}
    {{profile_image_form}}
    <button type="submit">Submit</button>
</form>

Error code

when I print the form.is_valid I got below lines.don't know Why image field is validating 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)>>
Ad

Answer

There are quite a few things wrong here. For example, your profile_form doesn't even seem to be a form.

But your immediate error is that you're not passing the FILES data to the image form. It should be:

    profile_image_form = self.profile_image_class(request.POST, request.FILES)

(Note there's no point passing an empty instance, just leave that out.)

Also make sure that in your template the form has the right enctype:

<form action="" method="post" enctype="mulitpart/form-data">
Ad
source: stackoverflow.com
Ad