3
I’m using Brcpffield from the localflavors package to validate the CPF in a form, it’s working, the problem is that in the template when I type in 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’s the best way to do it?
# 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%}
worked out, thanks :)
– E-Deslan
You are welcome. If you feel that the answer has answered your question, you can accept it as an answer by marking beside :)
– Paulo