It’s difficult to tell with your spacing in the email. The function needs to be inside the class, not outside.
From: [email protected] [mailto:[email protected]] On Behalf Of [email protected] Sent: Tuesday, October 16, 2018 4:34 AM To: Django users Subject: FormModel not validating the fields I have created a form using form Model and added the def_clean method but the clean method is not being called . Here are my files models.py class EmployeeData(models.Model): Yes = "Yes" No = "No" Onboard='Onboard' Left='Left' Is_GUEST_CHOICES = ( (Yes, 'Yes'), (No, 'No'), ) STATUS_CHOICES = ( (Onboard, 'Onboard'), (Left, 'Left'), ) gslab_id = models.CharField(max_length=20) name = models.CharField(max_length=100) email = models.CharField(max_length=100) is_guest = models.CharField(max_length=50,choices=Is_GUEST_CHOICES,default=No) status = models.CharField(max_length=50,choices=STATUS_CHOICES,default=Onboard) password = models.CharField(max_length=100,null=True) updated_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True) Forms.py from django import forms from .models import EmployeeData from django.core.exceptions import ValidationError class LoginForm(forms.ModelForm): class Meta: model = EmployeeData fields = ('gslab_id', 'password',) gslab_id= forms.CharField(widget=forms.TextInput(attrs = {'class':'form-control white-text'})) password= forms.CharField(widget=forms.PasswordInput(attrs = {'class': 'form-control white-text'})) def clean_gslab_id(self, *args, **kwargs): user_id = self.cleaned_data.get('gslab_id') if not "cfe" in user_id: raise forms.ValidationError('This is not valid title') return user_id views.py def getLogindata(request): if request.method == 'POST': login_form = LoginForm(request.POST or None) # return HttpResponse(login_form['gslab_id']) if login_form.is_valid(): emp_model=EmployeeData() emp_model.gslab_id=login_form['gslab_id'] emp_model.password=login_form['password'] emp_model.save() return redirect("/home") else: context={ 'login_form':login_form } return render(request, 'login.html', context) template <form name="loginForm" method="post" action="/processLogin/"> {% csrf_token %} {# Heading #} <h3 class="white-text text-center"> <strong>Login</strong> </h3> {% if login_form.errors %} {% for field in form %} {% for error in field.errors %} <p>{{ error}}</p> {% endfor %} {% endfor %} {% endif %} <hr> <div class="md-form"> <i class="fa fa-user prefix white-text"></i> {% if login_form %} {{ login_form.gslab_id }} {% endif %} <label for="username" class="white-text">Employee Id</label> </div> <div class="md-form"> <i class="fa fa-lock prefix white-text"></i> {% if login_form %} {{ login_form.password }} {% endif %} <label for="password" class="white-text">Password</label> </div> <div class="text-center"> <button type="submit" class="btn btn-indigo custom-button">Login</button> </div> </form> -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]<mailto:[email protected]>. To post to this group, send email to [email protected]<mailto:[email protected]>. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6747a03f-a0d2-48e7-b5ce-52d0c1a2153e%40googlegroups.com<https://groups.google.com/d/msgid/django-users/6747a03f-a0d2-48e7-b5ce-52d0c1a2153e%40googlegroups.com?utm_medium=email&utm_source=footer>. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e8b3943a67f94bfa87af793b967aaba8%40ISS1.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.

