Django 表单数据未保存到 databsae 中



我在Web应用程序上有一个表单,用于获取数据并存储到数据库中。 问题是: 我正在使用不同的字段以及 2 个日期字段。当我从模型和表单中删除 2 个日期字段时,表单数据很容易存储到数据库中,但当再次添加到模型和表单中时,它不会将任何内容保存到数据库中。

型:

name = models.CharField('Name', max_length=50, help_text='Co-worker name.')
email = models.EmailField('Email', help_text='Co-worker email.')
address = models.TextField('Address', help_text='Co-worker address.')
phone = models.CharField('Phone Number', max_length=20, help_text='Co-worker phone number.')
companyName = models.CharField('Company Name', max_length=80, help_text='Co-worker company name.', null=True,
blank=True)
workingLocation = models.CharField('Working Location', max_length=50,
help_text='Co-worker working '
'location.')
workingShift = models.CharField('Working Shift', max_length=50, help_text='Co-worker working shift.', default='')
workingSpace = models.CharField('Working Space', max_length=50, help_text='Co-worker working space.', default='')
teamMembers = models.CharField('Team Members', max_length=15, help_text="Co-Worker's Team Size.", default='')
coworkerPicture = models.ImageField('Co-Worker Picture', upload_to='../media/images/co-woker-pictures'
, help_text='Co-worker Picture.', default='profile_pics/default.jpg', )
joiningDate = models.DateField('Joining Date', help_text='Joining Date of Co-worker',default=date.today,
)
dob = models.DateField('Date of Birth', help_text='Date of Birth of Co-worker',  default=date.today,

视图:

def Coworkers(request):
# If user is not logged In
if not request.user.is_authenticated:
return redirect('login')
if request.method == 'POST':
form = addCoWorkerForm(request.POST, request.FILES)
print(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Co-Worker added successfully.')
return redirect('admin/co-workers')
c = context = ({
'form': form,
})
# Render the HTML template co_workers.html
return render(request, 'dashboard/co_workers.html', c)  

形式:

class addCoWorkerForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control formInput',
'placeholder': 'Name',
'required': 'True'
}))
email = forms.EmailField(widget=forms.EmailInput(attrs={
'class': 'form-control formInput',
'placeholder': 'Email',
'required': 'True'
}))
address = forms.CharField(widget=forms.Textarea(attrs={
'class': 'form-control formInput',
'placeholder': 'Address',
'rows': '4'
}))
companyName = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control formInput',
'placeholder': 'Company Name',
}))
phone = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control formInput',
'placeholder': 'Phone Number',
'required': 'True'
}))
CHOICES = [('Choose working location..', 'Choose working location..'),
('Islamabad', 'Islamabad'),
('Rawalpindi', 'Rawalpindi')]
workingLocation = forms.ChoiceField(choices=CHOICES, widget=forms.Select(attrs={
'class': 'form-control formInput',
'required': 'True',
}))
CHOICES = [('Choose working shift..', 'Choose working shift..'),
('Day Time', 'Day Time'),
('Night', 'Night')]
workingShift = forms.ChoiceField(choices=CHOICES, widget=forms.Select(attrs={
'class': 'form-control formInput',
'required': 'True',
}))
CHOICES = [('Choose working space..', 'Choose working space..'),
('Shared Space', 'Shared Space'),
('Private Room', 'Private Room')]
workingSpace = forms.ChoiceField(choices=CHOICES, widget=forms.Select(attrs={
'class': 'form-control formInput',
'required': 'True',
}))
teamMembers = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control formInput',
'placeholder': 'Team Members',
'required': 'True'
}))
coworkerPicture = forms.ImageField(widget=forms.FileInput(attrs={
'class': 'form-control formInput',
}))
joiningDate = forms.DateField(widget=forms.DateInput(attrs={
'class': 'form-control formInput',
'id': 'datePicker',
'input_formats': ['%d/%m/%Y %H:%M']
}))
dob = forms.DateField(widget=forms.DateInput(attrs={
'class': 'form-control formInput',
'id': 'datePicker',
'input_formats': ['%d/%m/%Y %H:%M']
}))
class Meta:
model = CoWorker_Data
fields = '__all__'

请向我建议解决方案。

您的input_formats用于日期时间字段,但您已将该字段定义为仅日期字段,因此请在此处更改如下:

joiningDate = forms.DateField(widget=forms.DateInput(attrs={
'class': 'form-control formInput',
'id': 'datePicker',
input_formats= settings.DATE_INPUT_FORMATS
}))

编辑:如果您在 settings.py 中定义date_input_format,请像这样定义它

DATE_INPUT_FORMATS = ['%d/%m/%Y']

注意:settings.py 中的DATE_INPUT_FORMATSUSE_I18N = True没有影响。

相关内容

  • 没有找到相关文章

最新更新