Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 表单日期字段在django中将None传递给post数据表单_Python_Django - Fatal编程技术网

Python 表单日期字段在django中将None传递给post数据表单

Python 表单日期字段在django中将None传递给post数据表单,python,django,Python,Django,我对捕获出生日期的日期字段使用boostrap datepicker,我遇到的问题是datefield没有返回任何值,这导致了一个错误(1048,“列'st_dob'不能为null”) model.py st_dob = models.DateField() st_dob = forms.DateField(label='Date Of Birth', widget=forms.DateInput(attrs={'class': 'dob_datepicker'}))

我对捕获出生日期的日期字段使用boostrap datepicker,我遇到的问题是datefield没有返回任何值,这导致了一个错误(1048,“列'st_dob'不能为null”)

model.py

    st_dob = models.DateField()
    st_dob = forms.DateField(label='Date Of Birth', widget=forms.DateInput(attrs={'class': 'dob_datepicker'}))
    if request.method == 'POST':

    if student_form.is_valid():
        print(request.POST.get('st_dob'))
forms.py

    st_dob = models.DateField()
    st_dob = forms.DateField(label='Date Of Birth', widget=forms.DateInput(attrs={'class': 'dob_datepicker'}))
    if request.method == 'POST':

    if student_form.is_valid():
        print(request.POST.get('st_dob'))
view.py

    st_dob = models.DateField()
    st_dob = forms.DateField(label='Date Of Birth', widget=forms.DateInput(attrs={'class': 'dob_datepicker'}))
    if request.method == 'POST':

    if student_form.is_valid():
        print(request.POST.get('st_dob'))
我的那部分约会挑肥拣瘦的人是这样的

 $('.dob_datepicker').datepicker({
         endDate: new Date(),
         autoclose: true, // close after insert
         format: 'yyyy-mm-dd',
     });
因此,它用于获取其他post数据,但对于此datefield,它不返回任何数据

#编辑

我使用这个:print(student_form.cleaned_data['st_dob'])进行编辑,虽然我得到了发布的数据,但问题是我的save函数总是给出空dob

class StudentSignUpForm(UserCreationForm):
st_reg_no = forms.CharField(label='Student Reg No', max_length=200, required=True)
st_other_name = forms.CharField(label='Other Name', max_length=200, required=False)
st_dob = forms.DateField(label='Date Of Birth', widget=forms.DateInput(attrs={'class': 'dob_datepicker'}))
# st_dob = forms.CharField(label='Date Of Birth', widget=forms.TextInput(attrs={'class': 'dob_datepicker'}))
gender_choice = (
    ('M', 'Male'),
    ('F', 'Female')
)

st_gender = forms.ChoiceField(label='Gender', choices=gender_choice)

st_phone_number = forms.CharField(label='Phone Number', max_length=200, required=False)
st_nationality = forms.CharField(label='Nationality', max_length=70)
st_city = forms.CharField(label='City', max_length=70)
programme = ProgrammeChoiceField(queryset=Programme.objects.all())
campus = CampusChoiceField(queryset=Campus.objects.all())
st_image = forms.ImageField(label='Upload Picture', required=False)

# for Hiding helptext for userCreationForm
def __init__(self, *args, **kwargs):
    super(StudentSignUpForm, self).__init__(*args, **kwargs)

    for fieldname in ['username', 'email', 'password1', 'password2']:
        self.fields[fieldname].help_text = None

class Meta(UserCreationForm.Meta):
    model = User
    fields = ('username', 'email', 'password1', 'password2', 'first_name', 'last_name')
@transaction.atomic
def save(self):
    # user = super().save(commit=False)
    user = super(StudentSignUpForm, self).save(commit=False)
    user.is_student = True
    user.save()
    # get instance of user above and adding it to student table
    student = Student.objects.create(user=user)
    student.st_reg_no = self.cleaned_data.get('st_reg_no')
    student.st_other_name = self.cleaned_data.get('st_other_name')
    student.st_dob = self.cleaned_data.get('st_dob')
    student.st_gender = self.cleaned_data.get('st_gender')
    student.st_phone_number = self.cleaned_data.get('st_phone_number')
    student.st_nationality = self.cleaned_data.get('st_nationality')
    student.st_city = self.cleaned_data.get('st_city')
    student.programme = self.cleaned_data.get('programme')
    student.campus = self.cleaned_data.get('campus')
    # student.st_image = self.cleaned_data.get('st_image')
    student.save()
    return user

不要认为这是您的问题,但您应该从
表单中获取视图中的值。已清理的数据['st\u dob']
,而不是从request.POST。您曾经解决过此问题吗?如果是,您是如何解决的