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