FormModel not validating the fields

19 views
Skip to first unread message

faizan...@gslab.com

unread,
Oct 16, 2018, 10:38:39 AM10/16/18
to Django users
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>


Matthew Pava

unread,
Oct 16, 2018, 10:53:04 AM10/16/18
to django...@googlegroups.com

It’s difficult to tell with your spacing in the email.  The function needs to be inside the class, not outside.

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.

Andréas Kühne

unread,
Oct 16, 2018, 10:53:21 AM10/16/18
to django...@googlegroups.com
Hi,

Your code is a bit hard to read. Python is whitespace significant and you should have more than one space for indentation of methods in classes and so on.

From what I can see it doesn't look like the clean method is a method in the LoginForm class?
Check here for the official docs:

Also, there is no need for the *args and **kwargs on the method, because those will not be used.

Regards,

Andréas


Reply all
Reply to author
Forward
0 new messages