'CommentFrom' object is not callable

I am getting this error CommentFrom object is not callable after add validation in my forms. If I remove the validation then error will be gone but I need to be validate my forms before submitting. How to solve this problem?
here is my code:

froms.py

class CommentFrom(forms.ModelForm):

      

      class Meta:

          model = BlogComment

          fields = ['name','email','comment','parent']

          widgets = {

            'name': forms.TextInput(attrs={'class':'form-control','placeholder': 'name'}),

            'email': forms.TextInput(attrs={'class':'form-control','placeholder': 'email'}),

            'comment': forms.Textarea(attrs={'class':'form-control','placeholder': 'comment'}),

         

      }

      def clean_name(self): #If I remove this validation then error will be gone 

         name = self.cleaned_data['name']

         if len(name) > 10:

             raise ValidationError("Maximum 250 character allowed in Title")

         return name

views.py

class BlogDetail(DetailView):

      model = Blog

      template_name = 'blog_details.html'      

      

      def get(self,request,slug):

          blog = Blog.objects.get(slug=slug)

          queryset = BlogComment.objects.filter(is_published="0",blog=blog)

          

          form = CommentFrom()

          context = {'form':form,

                     'blog':blog,

                     'queryset':queryset,

                     

                   }

                   

          return render(request,'blog_details.html',context)

         

      def post(self,request,slug):

          blog = Blog.objects.get(slug=slug)

          form = CommentFrom(request.POST)

              

          if form.is_valid():

             comment = form.save(commit=False) 

             comment.blog = blog

             comment.save()

             messages.add_message(self.request, messages.INFO, 'Your Comment pending for admin approval')

             return redirect(reverse('blog-detail', kwargs={'slug':slug}))

          

          else:

               form()

                    

          context = {'form':form,

                     'blog':blog,

                                       

                   }

          return render(request,'blog_details.html',context)

error message:

  File "P:\django\farhyn\blog\views.py", line 48, in post
    form()
TypeError: 'CommentFrom' object is not callable

Here is the precise cause of the error. Your variable, form here is an instance of a CommentFrom object and can’t be just “called”. It’s not clear why you have this, so I can’t offer any suggestions as to what it should be, but that is a source of an error.

I’d guess that the reason it’s showing up with the validation is that the validation is failing for some reason, causing the is_valid test to fail, therefore executing the else clause.

KenWhitesell Thanks for your message. I am using else for rendering empty froms if it’s not valid.

Then you need to create an instance of the form that is empty: form = CommentFrom().

KenWhitesell previous error gone after add form = CommentFrom() now I am getting this error

Failed lookup for key [queryset] in [{'True': True, 'False': False, 'None': None}, {}, {}, {'form': <CommentFrom bound=False, valid=False, fields=(name;email;comment;parent)>, 'blog': <Blog: author name : tusar | admin Post>}]

If I remove queryset from my template then error gone but I must be use queryset in my template. I am using queryset for review every comment before it published or visible in my template. I am using this queryset in my post method:
queryset = BlogComment.objects.filter(is_published="0",blog=blog)

hi KenWhitesell

What do you need to do to have some specific variable or object available to be rendered within the template?

(What do you do with your other variables to make them available?)

KenWhitesell what do you mean by other variables ?

This is a general question, not specifically related to what you’re reporting here.

What do you do in a view to make variables available to the rendering engine to be used in templates? (Or, to phrase the same question somewhat differently, how do you pass data to the rendering engine to be used within a template?)

I solved the problem after switch on function based view. I think I did any mistake in class based view. After switch on function based view my problems was solved.

If you think the problem was simply with the switch between a CBV and an FBV, then you’ve learned the wrong lesson from your solution.

Yes, you may have gotten it working now, but if you can’t answer the previous question posed to you, you’re missing an important element in your understanding of how Django works.

KenWhitesell specifically The problems was occurring for using MPTT in my template. I don’t have enough knowledge on MPTT. I was using queryset with MPTT in my template

The error you reported most recently has nothing specifically to do with MPTT. Understanding the issue Failed lookup for key [queryset] in ... is fundamental to understanding how the rendering of templates work.

1 Like

KenWhitesell you want to say it was rendering related issue ?

This is the question being referenced. Understanding the answer to this is important.

1 Like