Formatting django form.non_field_errors in a template

How do I format django form.non_field_errors.as_text in a template so that they are not an unordered list or are not added * forward?

{{ form.non_field_errors.as_text }} displays errors with * before the text.

This django ticket also helped explain why * would not be deleted, but that will not help me. I do not want * .

Both {{form.non_field_errors}} and {{form.non_field_errors.as_ul}} appear as an unordered list, and I don't want an unordered list.

+6
source share
3 answers
 {% for error in form.non_field_errors %} {{error}} {% endfor %} 

When you call up the error list as text, it tries to display it as a list. Just go through the list to get the error yourself so you can apply your own style.

More on this on the django project

+17
source

Well, by default, Django forms use ErrorList as error_class ( evidential link ). As you can see, its as_text format formats the list by adding asterisks to the values.

So, you can create a custom error_class with your own as_text method and submit it to your form accordingly.

0
source

form.non_field_errors according to form.non_field_errors is not always the best approach, for example, in my case I wanted to display a tooltip, as in this screenshot, next to the field under consideration, using the Django Widget settings .

project / application / templates / template.html

 {% render_field field class="form-control is-invalid" data-toggle="tooltip" title=field.errors.as_text %} 

Instead of messing around with the template code to pass the HTML title attribute in one piece, I followed @oblalex's advice and wrote my own modified ErrorList class with as_text method without asterisks.

project / application /utils.py

 from django.forms.utils import ErrorList class MyErrorList(ErrorList): """ Modified version of the original Django ErrorList class. ErrorList.as_text() does not print asterisks anymore. """ def as_text(self): return '\n'.join(self) 

Now that the as_text() function is overwritten, you can pass your MyErrorList class as the error_class argument of your Form or ModelForm or, as in my case, some set of ModelForm forms:

project / application /views.py

 from django.forms import formset_factory from .forms import InputForm from .utils import MyErrorList def yourView(request): InputFormSet = formset_factory(InputForm) formset = InputFormSet(error_class=MyErrorList) context = {'formset': formset} return render(request, 'template.html', context) 

And now the hint looks like this without an asterisk.

Instead of passing error_class every time you create an instance of the form in your views, you can simply add this single- line line to your form class (see This answer from @Daniel ):

project / application /forms.py

 from django import forms from .utils import MyErrorList class InputForm(forms.ModelForm): def __init__(self, *args, row, **kwargs): super(InputForm, self).__init__(*args, **kwargs) self.error_class = MyErrorList 
0
source

All Articles