我一直在到处研究这个问题的答案,但我只是想在我的models.py上创建的DateField中添加一个小部件,在那里您可以看到实际的日历,就好像您直接通过输入类型=date的html来做一样。由于我有几个日期字段,这就成了一个问题,因为它们都需要与表单和小部件的其余部分具有相同的格式。感觉问题已经得到了答案,但我找到的答案或东西都没有返回正确的答案。
models.py
class InfoPersonal(models.Model):
Fecha = models.DateField()
cargo_act = models.CharField(max_length=100)
Nombres_y_Apellidos_completos = models.CharField(max_length=100)
Lugar = models.CharField(max_length=100)
Fecha_de_Nacimiento = models.DateField(null=True)
Discapacidad= models.BooleanField()
grado = models.CharField(max_length=100, blank=True)
Edad = models.IntegerField(validators=[MinValueValidator(18), MaxValueValidator(80)])
Tipo_de_Sangre = models.CharField(max_length=50, choices=sangre_choice)
Estatura = models.FloatField(validators=[MaxValueValidator(3.0), MinValueValidator(0.5)])
Direccion_Domicilio_actual = models.CharField(max_length=100)
Manzana = models.CharField(max_length=100)
Villa = models.CharField(max_length=100)
parroquia = models.CharField(max_length=100)
Telefono_Domicilio = models.IntegerField(blank=True, null=True)
Telefono_Celular = models.IntegerField(blank=True, null=True)
Telefono_Familiar = models.IntegerField(blank=True, null=True)
cedula = models.IntegerField()
estado_civil = models.CharField(max_length=50, choices=list_estado_civil)
#Conyuge
Nombre_completo_del_conyuge= models.CharField(max_length=100,blank=True)
Direccion_Domiciliaria=models.CharField(max_length=100,blank=True)
Telefono=models.IntegerField(blank=True, null=True)
Cedula_de_Identidad=models.IntegerField(blank=True, null=True)
Fecha_de_NacimientoC=models.DateField(blank=True, null=True)
Direccion_Trabajo=models.CharField(max_length=100,blank=True)
Telefono_del_trabajo=models.IntegerField(blank=True,null=True)
#Hijos
Nombres= models.CharField(max_length=100,blank=True)
Lugar_y_Fecha_de_NacimientoH = models.CharField(max_length=100,blank=True)
Esposo_con_Discapacidad = models.BooleanField(blank=True)
Hijos_con_Discapacidad= models.BooleanField(blank=True)
#InfoFamiliares
Apellidos_y_Nombres_1= models.CharField(max_length=100,blank=True)
Telefono_Familiar_1 = models.IntegerField(blank=True,null=True)
Fecha_Nacimiento_1 = models.DateField(blank=True,null=True)
Relacion_de_Parentesco_1 = models.CharField(max_length=100,blank=True)
Apellidos_y_Nombres_2= models.CharField(max_length=100,blank=True)
Telefono_Familiar_2 = models.IntegerField(blank=True,null=True)
Fecha_Nacimiento_2 = models.DateField(blank=True,null=True)
Relacion_de_Parentesco_2 = models.CharField(max_length=100,blank=True)
Apellidos_y_Nombres_3= models.CharField(max_length=100,blank=True)
Telefono_Familiar_3 = models.IntegerField(blank=True,null=True)
Fecha_Nacimiento_3 = models.DateField(blank=True,null=True)
Relacion_de_Parentesco_3 = models.CharField(max_length=100,blank=True)
Apellidos_y_Nombres_4= models.CharField(max_length=100,blank=True)
Telefono_Familiar_4 = models.IntegerField(blank=True, null=True)
Fecha_Nacimiento_4 = models.DateField(blank=True,null=True)
Relacion_de_Parentesco_4 = models.CharField(max_length=100,blank=True)
Trabajan_familiares = models.BooleanField(blank=True)
Trabajan_Amistades = models.BooleanField(blank=True)
#estudiosRealizados
Primaria=models.CharField(max_length=100)
Lugar_Primaria= models.CharField(max_length=100)
Curso_Primaria= models.CharField(max_length=100)
Año_Primaria=models.IntegerField()
Titulo_Primaria=models.CharField(max_length=100)
Secunadaria=models.CharField(max_length=100)
Lugar_Secundaria=models.CharField(max_length=100)
Curso_Secundaria=models.CharField(max_length=100)
Año_Secundaria=models.IntegerField()
Titulo_Secundaria=models.CharField(max_length=100)
Superior=models.CharField(max_length=100)
Lugar_Superior=models.CharField(max_length=100)
Curso_Superior=models.CharField(max_length=100)
Año_Superior=models.IntegerField()
Titulo_Superior=models.CharField(max_length=100)
Otros=models.CharField(max_length=100,blank=True)
Lugar_Otros=models.CharField(max_length=100,blank=True)
Curso_Otros=models.CharField(max_length=100,blank=True)
Año_Otros=models.IntegerField(blank=True, null=True)
Titulo_Otros=models.CharField(max_length=100,blank=True)
idioma=models.CharField(max_length=100)
forms.py
class Form_InfoPersonal(ModelForm):
class Meta:
model = InfoPersonal
fields = '__all__'
widgets = {
'Nombres_y_Apellidos_completos': forms.TextInput(attrs={'class':'form-control form-control-lg mt-3 ml-4 w-75','required':True}),
'cargo_act': forms.TextInput(attrs={'class':'form-control form-control-lg mt-3 ml-4 w-75','required':True}),
'Lugar': forms.TextInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'Fecha': forms.DateField(widget=NumberInput(attrs={'type':'date'})),
'grado': forms.TextInput(attrs={'class':'form-control form-control-lg mt-3 ml-4 w-75','required':False}),
'Edad' : forms.NumberInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'Tipo_de_Sangre' : forms.TextInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75','required':True}),
'Estatura': forms.NumberInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'Direccion_Domicilio_actual': forms.TextInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'Manzana':forms.TextInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'Villa': forms.TextInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'parroquia': forms.TextInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'Telefono_Domicilio' : forms.NumberInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75' }),
'Telefono_Celular': forms.NumberInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'Telefono_Familiar': forms.NumberInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
'cedula': forms.NumberInput(attrs={'class': 'form-control form-control-lg mt-3 ml-4 w-75', 'required':True}),
}
views.py
def formu_view(request):
if request.method == 'POST':
form = Form_InfoPersonal(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Su formulario ha sido llenado y guardado correctamente')
return render(request, '')
else:
form= Form_InfoPersonal()
return render(request, 'users/formu.html', context={'form':form})
error message
Internal Server Error: /formu
Traceback (most recent call last):
File "C:UsersricarAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangocorehandlersexception.py", line 55, in inner
response = get_response(request)
File "C:UsersricarAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangocorehandlersbase.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:DescargasRP3 trabajoRP3 trabajousersviews.py", line 247, in formu_view
if form.is_valid():
File "C:UsersricarAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoformsforms.py", line 205, in is_valid
return self.is_bound and not self.errors
File "C:UsersricarAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoformsforms.py", line 200, in errors
self.full_clean()
File "C:UsersricarAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoformsforms.py", line 437, in full_clean
self._clean_fields()
File "C:UsersricarAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoformsforms.py", line 444, in _clean_fields
value = bf.initial if field.disabled else bf.data
File "C:UsersricarAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoformsboundfield.py", line 127, in data
return self.form._widget_data_value(self.field.widget, self.html_name)
File "C:UsersricarAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoformsforms.py", line 224, in _widget_data_value
return widget.value_from_datadict(self.data, self.files, html_name)
AttributeError: 'DateField' object has no attribute 'value_from_datadict'
为小部件使用了错误的类。把
'Fecha': forms.DateField(widget=NumberInput(attrs={'type':'date'})),
'Fecha': forms.DateInput(widget=NumberInput(attrs={'type':'date'})),
^^^^^^^^^
forms.DateField
用于声明表单的字段。它没有验证所需的方法value_from_datadict()
。forms.DateInput
是用于日期输入的小部件。它有所需的方法。