xxxxxxxxxx
#non-field errors ca be accessed in html via
{{form.non_field_errors}}
# or
{% if form.non_field_errors %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
#Remember to include raising this error in your forms.py file
xxxxxxxxxx
GITHUB URL: https://stackoverflow.com/questions/38847441/django-exception-handling-best-practice-and-sending-customized-error-message
form = TestForm(request.POST)
if form.is_valid():
else:
message = "The form has errors"
explanation = form.errors.as_data()
# Also incorrect request but this time the only flag for you should be that maybe JavaScript validation can be used.
status_code = 400
xxxxxxxxxx
# In your Django view
from django.shortcuts import render
from .forms import MyForm
def my_view(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
# Process the form data
return render(request, 'success.html')
else:
form = MyForm()
# Display the form including any errors
return render(request, 'form.html', {'form': form})