So, I was learning Django from a tutorial and came across form validation. The tutor's version had errors pop-up on screen when validation failed but nothing shows up on my form.
Here is my forms.py.
from django import forms
from django.core import validators
def check_for_z(value):
if value[0].lower() != 'z':
raise forms.ValidationError('Name should start with z')
class FormName(forms.Form):
name = forms.CharField(validators = [check_for_z])
email =forms.EmailField()
text = forms.CharField(widget=forms.Textarea)
This is my views.py file.
from django.shortcuts import render
from myformapp import forms
def form_name_view(request):
form = forms.FormName()
if request.method == 'POST':
filled_form = forms.FormName(request.POST)
if filled_form.is_valid():
# print the form data to terminal
print("Validation success")
print('Name: ' + filled_form.cleaned_data['name'])
print('Email: ' + filled_form.cleaned_data['email'])
print('Text: ' + filled_form.cleaned_data['text'])
return render(request, 'myformapp/formpage.html', {'form' : form})
And this is my template for the page.
<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Form Page</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div class="container">
<form method="post">
{{form.as_p}}
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="Submit">
</form>
</div>
</body>
</html>
So whenever i enter a name that's not beginning with z i am supposed to get an exception on the screen but nothing shows up. My code is pretty similar to what the tutor is showing on his machine. Can someone point me in the right direction as to what i am doing wrong. Thanks