表单日期字段传递无以在Django中发布数据表单

时间:2019-01-21 11:49:08

标签: python django

我将boostrap datepicker用于捕获出生日期的日期字段,我遇到的问题是日期字段返回None导致错误(1048,“ Column'st_dob'不能为null”)。

model.py

    st_dob = models.DateField()

forms.py

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

view.py

    if request.method == 'POST':

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

我的boostrap日期选择器部分看起来像这样

 $('.dob_datepicker').datepicker({
         endDate: new Date(),
         autoclose: true, // close after insert
         format: 'yyyy-mm-dd',
     });

因此它可以获取其他帖子数据,但是对于此日期字段,它不返回任何信息

#Edits

我使用以下命令进行了编辑:print(student_form.cleaned_data ['st_dob'])尽管我得到了发布的数据,但问题是我的保存函数始终给出空的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

0 个答案:

没有答案