Hi, I’m trying to make a custom error in a form in a class based view (CreateView). I have read the documentation and it is supposed that with the “clean()” function you can add errors, but my problem is that the form is submitted even when the error should appear.
this is my class:
class RecommendationCreateView(LoginRequiredMixin, CreateView): model = Recommendation template_name = 'recommendation/recommendation_new.html' form_class = NewPostForm success_url = reverse_lazy('home') def get_form_kwargs(self): """ Passes the request object to the form class. This is necessary to only display members that belong to a given user""" kwargs = super(RecommendationCreateView, self).get_form_kwargs() kwargs['request'] = self.request.user return kwargs def post(self, request): super(RecommendationCreateView, self).post(request) to_user=request.POST['to_user'] to_user = user_model.objects.filter(id = to_user).get() hobby = request.POST['hobby'] hobby = Hobby.objects.filter(id = hobby).get() recom = Recommendation.objects.create(from_user=request.user, to_user=to_user, hobby=hobby, text=request.POST['text']) recom.save() return redirect('recommendations')
this is my form:
class NewPostForm(forms.ModelForm): def clean(self): super(NewPostForm, self).clean() to_user = self.cleaned_data.get('to_user') to_user = user_model.objects.filter(username=to_user.username).get() if to_user.username == 'NAME': self._errors['to_user'] = 'Custom error to display' return self.cleaned_data class Meta: model = Recommendation fields = ('from_user', 'to_user', 'hobby', 'text') widgets = {'from_user': forms.HiddenInput()}
this is my model:
class Recommendation(models.Model): id = models.UUIDField( primary_key = True, default = uuid.uuid4, editable = False ) from_user = models.ForeignKey( get_user_model(), on_delete=models.CASCADE, related_name="recommendation_request_sent", ) to_user = models.ForeignKey( get_user_model(), on_delete=models.CASCADE, related_name="recommendation_requests_received", ) hobby = models.ForeignKey(Hobby, on_delete=models.CASCADE) text = models.TextField(null = True, blank = True) def get_absolute_url(self): return reverse('recommendations')
My goal is that when I make a recommendation if I send the form with the field “to_user” with the name “NAME” I get a custom error in the form. I enter the function “clean” and enters the if when they are the same but the form is sent anyway.
How can I stop the form from being sent? I have tried other functions like “form_valid()” but it never enters this function.