def clean_title(self):
title = self.cleaned_data['title']
if len(title) < 5:
raise forms.ValidationError("Please write more in Title.")
return title
In my models.py, this is usually how I set an error form my title.
However, what if I want to do it in views.py? I want to set an error like that, except in the logic of my code.
Form errors are stored in a dictionary (ErrorDict from
django.forms.util) that maps field name to ErrorList class. Look at _clean_fields method – you would need to call your formclean()method, then delete appropriate field fromcleaned_dataattribute and do something like this: