I'm using the BRCPFField of the localflavors package to validate the CPF in a form, it's working, the problem is that in the template when I type an invalid CPF that django error page appears, I just wanted a message to be displayed and the person could change the field again ... What is the best way to do this?
# form:
class ClienteForm(forms.ModelForm):
cpf = BRCPFField()
class Meta:
model = Cliente
fields = ('nome', 'cpf', 'celular', 'tel_fixo', 'email', 'end', 'data_cadastro')
# view:
def cadastrar_cliente(request):
if request.method == 'POST':
form = ClienteForm(request.POST)
if form.is_valid():
cliente = form.save()
cliente.save()
return redirect('/calcados/', {})
else:
form = ClienteForm()
return render(request, 'calcados/cadastrar.html', {'form': form})
# template:
{% extends 'calcados/base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Salvar</button>
</form>
{% endblock%}