I'm trying to show my ValidationError from clean_MY_VALUE in the template.
*template*
<form action="{% url 'MY_VIEW_CLASS' MY_ARG %}" method="POST">{% csrf_token %}
{{form.as_p}}
<button type="submit">Submit</button></form>
{{ form.errors }}
{{ form.non_field_errors }}
*views.py*
def MY_VIEW_CLASS(request, MY_ARG):
MY_ARG = "query to check for silly conditions"
if request.method=='POST':
form = MY_FORM(request.POST)
if form.is_valid():
MY_VALUE = form.cleaned_data['MY_VALUE']
# do more stuff
*forms.py*
class MY_FORM(forms.Form):
MY_VALUE = forms.CharField(
required = True,
max_length = 70,
)
def __init__(self, *args, **kwargs):
self.MY_ARG = kwargs.pop('MY_ARG', None)
super(MY_FORM, self).__init__(*args, **kwargs)
def clean_MY_VALUE(self):
data = self.cleaned_data['MY_VALUE']
MY_ARG = self.MY_ARG
if MY_VALUE and MY_ARG == True:
raise ValidationError("this is the error message.")
if any(self.errors):
return self.errors
# return the cleaned data
return data
*Debug*
What's weird is that when I debug, I cannot see the ValidationError I just
created in errors.
(Pdb) raise forms.ValidationError("That is causing an error.")***
django.core.exceptions.ValidationError: ['That is causing an error.']
(Pdb) self.errors{}
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/e9fbe8a8-35f3-4f30-9575-919a91bb9579%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.