I am building a very simple form in Django, and want to display form errors in a Bootstrap alert tag. I know how to do this (e.g. Django docs or Django Forms: if not valid, show form with error message or How to render Django form errors not in a UL?
). However, when I do, I am seeing the errors appear twice. It seems like Django's {{ form }}
element in the template is displaying the errors in a ul
tag by default, in addition to my custom-formatted errors.
What is the best way to avoid this duplication?
In template.html
:
<!--Load the file search form from the view-->
<form action="" method="post" id="homepage_filesearch">
<!--Show any errors from a previous form submission-->
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% endif %}
{{ csrf_input }}
{{ form }}
<button class="btn btn-primary" type="submit">Search</span></button>
</form>
In views.py
:
from .forms import FileSearchForm
def view(request):
# Create a form instance and populate it with data from the request
form = FileSearchForm(request.POST or None)
# If this is a POST request, we need to process the form data
if request.method == 'POST':
if form.is_valid():
return form.redirect_to_files()
template = 'template.html'
context = {'form': form}
return render(request, template, context)
In forms.py
:
class FileSearchForm(forms.Form):
# Define search field
search = forms.CharField(label='', max_length=500, required=True,
empty_value='Search')
def clean_search(self):
# Get the cleaned search data
search = self.cleaned_data['search']
# Make sure the search is either a proposal or fileroot
if some_error_case:
raise forms.ValidationError('Invalid search term {}. Please provide proposal number or file root.'.format(search))