无法将日期选择器小部件中的日期保存到我的数据库中

时间:2018-09-27 18:26:52

标签: django django-models django-forms

我是Django的新手。基本上我有一种形式,其中我有一个date_of_birth字段,我通过一个datepicker小部件输入它。但是我无法将其存储到我的数据库(SQLite)中。 这是我的model.py-:

class users(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
email = models.CharField(max_length=50)
dob = models.DateField(max_length=30)
phone = models.IntegerField()

def __str__(self):
    return self.name

这是forms.py-:

class DateInput(forms.DateInput):
input_type = 'date'
class user_form(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(),required=True,max_length=30)
email = forms.CharField(widget=forms.EmailInput(), required=True, max_length=50)
phone = forms.IntegerField(widget=forms.NumberInput)

dob = forms.DateField(widget=DateInput(attrs={'class':'datepicker'}))
class Meta():
    model = users

    fields = ['name', 'email', 'phone', 'dob']

这是View.py-:

def userdata(request):
if request.method == 'POST':
    form = user_form(request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/user-form/')
else:
    form = user_form()
return render(request, 'user_form.html',{'form':form})

0 个答案:

没有答案