19๐
โ
What about defining error_css_class?
http://docs.djangoproject.com/en/dev/ref/forms/api/#styling-required-or-erroneous-form-rows?
class MyForm(ModelForm):
error_css_class = 'error'
๐คerrx
10๐
To answer the original question.
You can add the desired class to the field in the view to where you are submitting your form and doing your form.is_valid()
check. Not the prettiest but it will work.
def submit_form(request):
if request.method = 'POST':
if. form.is_valid():
# Do something with clean form data
pass
else:
# Append css class to every field that contains errors.
for field in form.errors:
form[field].field.widget.attrs['class'] += ' my-css-class'
return render(request, submit_form.html, {
'form': form
})
๐คholmberd
- [Django]-POST jQuery array to Django
- [Django]-How to delete project in django
- [Django]-How to specify an IP address with Django test client?
3๐
Expanding upon errxโs answer.
Add the CSS
.error input, .error select {
border: 2px red solid;
}
to specifically highlight the field
๐คAugier
- [Django]-Django โ No module named _sqlite3
- [Django]-How do I deploy Django on AWS?
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
- [Django]-Using django-admin on windows powershell
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-Filtering dropdown values in django admin
1๐
template :
<div style="color:red">{{ userForm.short_description.errors.as_text }}</div>
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-Annotate a queryset with the average date difference? (django)
Source:stackexchange.com