my EmployeeForm类中“Employee\u Name”的自定义验证无效

2024-05-23 20:44:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

表单.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!!")

视图.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\u 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 %}

Tags: thenamepyformaddresscreateemployeeforms
2条回答

为了验证单个字段,django表单应该有一个clean_<field_name>格式的函数。在您的例子中,它是clean_Employee_Name

重命名该函数,将进行验证

验证方法必须是name clean\u Employee\u name,因为字段名是Employee\u 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!!")

希望有帮助

相关问题 更多 >