我的EmployeeForm类中对'Employee_Name'的自定义验证不起作用

时间:2019-02-19 10:14:56

标签: python django forms validation

我已经创建了一个表格来创建员工详细信息。在forms.py中,我制作了EmployeeForm类,还添加了一个自定义的验证来在那里验证“ Employee_Name”,但是自定义的验证“ EmployeeNameValidate”不起作用。

forms.py

class EmployeeForm (forms.ModelForm):
    Employee_Name = forms.CharField(widget=forms.TextInput(attrs{
                              "placeholder" : "Enter the Employee Name "
                                }
                                 ))
    Employee_ID   = forms.CharField(initial = '17CS')
    Address       = forms.CharField(
            required = False , 
            widget = forms.Textarea
                                  (
            attrs={
                 "placeholder": "Enter your Address here ",
                 "class" : "new-class-name two",
                 "id" :"my-id-for-textarea",
                 "rows":10,
                 "cols":100
                }
                ) 
                                   )
    Salary  = forms.DecimalField(initial = 60000.24)

    class Meta:
        model  = Employee
        fields = [
        'Employee_Name',
        'Employee_ID',
        'Address',
        'Salary'
        ]

    #print("yaaah !!")

    def EmployeeNameValidate(self, *args , **kwargs):
        print("yaaah 22222 !!")
        Employee_Name = self.cleaned_data.get("Employee_Name")
        print(Employee_Name)
        if "abc" not in Employee_Name:
            raise forms.ValidationError ("This is not a valid Employee Name 
                                        ")
        return Employee_Name 

     #print("yaaah 3333333333!!")

views.py

def emp_create_view(request):
    form = EmployeeForm(request.POST or None )
    if form.is_valid():
        form.save()
        form = EmployeeForm()
    context={
    'form': form
    }

    return render(request,"employee/emp_create.html",context)

emp_create.html

{% extends "base.html" %}
{% block content %}
<h1>
 Enter the details of the Employee :
</h1>
<form method = 'POST'> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save' />
</form>
{% endblock %}

2 个答案:

答案 0 :(得分:0)

您的验证方法必须为clean_Employee_Name,因为您的字段名称为Employee_Name

尝试

class EmployeeForm (forms.ModelForm):
    Employee_Name = forms.CharField(widget=forms.TextInput(attrs{
                              "placeholder" : "Enter the Employee Name "
                                }
                                 ))
    Employee_ID   = forms.CharField(initial = '17CS')
    Address       = forms.CharField(
            required = False , 
            widget = forms.Textarea
                                  (
            attrs={
                 "placeholder": "Enter your Address here ",
                 "class" : "new-class-name two",
                 "id" :"my-id-for-textarea",
                 "rows":10,
                 "cols":100
                }
                ) 
                                   )
    Salary  = forms.DecimalField(initial = 60000.24)

    class Meta:
        model  = Employee
        fields = [
        'Employee_Name',
        'Employee_ID',
        'Address',
        'Salary'
        ]

    #print("yaaah !!")

    def clean_Employee_Name(self, *args , **kwargs):
        print("yaaah 22222 !!")
        Employee_Name = self.cleaned_data.get("Employee_Name")
        print(Employee_Name)
        if "abc" not in Employee_Name:
            raise forms.ValidationError ("This is not a valid Employee Name 
                                        ")
        return Employee_Name 

     #print("yaaah 3333333333!!")

希望有帮助

答案 1 :(得分:0)

要验证单个字段,django表单应具有clean_<field_name>格式的函数。您的情况是clean_Employee_Name

重命名该函数,然后进行验证