我的表单中有一个名为生日的字段,类似于:
class Personal_info_updateForm(forms.Form):
birthdate = forms.DateField(widget=SelectDateWidget(years=[y for y in range(1930,2050)]))
..
..views.py
def personal_info(request):
mc = MenuCategories()
listCategories = mc.getCategories()
oe = OEConnector()
if request.method == 'POST':
f1 = Personal_info_updateForm(request.POST)
print request.POST
if f1.is_valid():
first_name = f1.cleaned_data['first_name']
last_name = f1.cleaned_data['last_name']
c=[last_name,first_name]
name = " ".join(c)
print name
birthdate = f1.cleaned_data['birthdate']
birthdate_year,birthdate_month,birthdate_day=['','','']
birthdate = [birthdate_year,birthdate_month,birthdate_day]
c=" ".join(birthdate)
print birthdate
title = f1.cleaned_data['title']
print title
email = f1.cleaned_data['email']
mobile = f1.cleaned_data['mobile']
phone = f1.cleaned_data['phone']
result = update_details(name,first_name,last_name,birthdate,email,mobile,phone)
print result
return HttpResponse('/Info?info="Congratulations, you have successfully updated the information with aLOTof"')一个1.html,我将整个表单调用为
<form action="" method="POST">
<table style="color:black;text-align:left; margin-left: 20px;">
{{ form.as_table }}
</table>
<input type="submit" value="UPDATE">
</form> 我希望这样可以在Postgresql中存储生日的值。但是它不起作用,所以我研究了把它转换成DateTime字段,因为日期字段对象是完全不同的。请告诉我如何转换,这样我才能解决这个问题。我把它当作一根线。
提前感谢
发布于 2011-03-29 08:38:45
按照django文档,http://docs.djangoproject.com/en/dev/ref/forms/fields/#datefield,date字段的值将正常化为Python对象。
因此,如果模型中有类似于birthdate = models.DateField()的内容,那么从表单中直接分配值应该是直接的。
#views.py
birthdate = f1.cleaned_data['birthdate']
my_model_instance.birthdate = birthdate但是,如果您仍然希望将其转换为DateTime,假设您已经将模型字段更改为DateTime,则可以:
对于第二个选项,您需要创建一个具有以下格式的datetime.datetime对象:
import datetime
bday = datetime.datetime(year, month, day)有关更多信息,请查看日期时间和[time][2]上的python。
https://stackoverflow.com/questions/5468377
复制相似问题