Django:“电子邮件”列中的空值违反了非空约束

时间:2019-12-17 15:47:52

标签: python django postgresql django-models django-rest-framework

models.py

class Employee(models.Model):
    name = models.Charfield(max_length=50)

    contact = models.BigIntegerField()

    email = models.EmailField(max_length=40, null=False)

    password = models.TextField(max_length=30)

form.py

class CreateEmployee(forms.Form):
    name = forms.CharField(label="Enter Your Name", max_length=30, required=True)

    contact = forms.IntegerField(required=True)

    Email = forms.EmailField(max_length=40, widget=forms.EmailInput(), required=True)

    password = forms.CharField(required=True, widget=forms.PasswordInput())

views.py

class Create_Employee(TemplateView):
    template_name = 'employee.html'

    def get(self, request, *args, **kwargs):

        form = CreateEmployee()

        return render(request, self.template_name, {'form': form})

    def post(self, request):

        try:

            data = request.POST.get

            Employee(name=data('name'),

                     contact=data('contact'),

                     email=data('email'),

                     password=data('password')

                     ).save()

            return HttpResponse("Employee has been added successfully", 200)

        except Exception as e:

            return HttpResponse("Failed: {}".format(e), 500)

提交表单后,出现此错误:

  

失败:“电子邮件”列中的空值违反了非空约束   详细信息:失败行包含(20,usman,31902355,null,123456s)。

0 个答案:

没有答案