视图.py
def when(request):
user = request.user
report = Report.objects.get(user=request.user)
reportform = ReportForm(instance=report)
settings = Settings.objects.get(user=request.user)
settingsForm = SettingsForm(instance=settings)
# settings=Settings.objects.get(user=2)
if settings.date_format == '0':
date = report.manual_date.strftime('%d/%m/%Y')
else:
date = report.manual_date.strftime('%m/%d/%Y')
if settings.time_format == '0':
time = report.manual_time.strftime('%I:%M%p')
else:
time = report.manual_time.strftime('%H:%M')
if request.method == 'POST':
reportform = ReportForm(instance=report,data=request.POST,initial={'manual_date': date,'manual_time': time})
if reportform.is_valid():
report = reportform.save(commit=False)
report.user = request.user
report.save()
return redirect('/member/media/')
return render_to_response('incident/when.html',{
'newreport_menu': True,
'form': reportform,
'date':date,
'time':time,
},
context_instance=RequestContext(request))
表格.py
class ReportForm(forms.ModelForm):
class Meta:
model = Report
fields = ['incident_description','manual_date','manual_time', 'location_description',
'incident_followup', 'incident_followup_name_1', 'incident_followup_email_1',
'incident_followup_name_2', 'incident_followup_email_2', 'phone_call_log',
'notes_other','notes_firstaid','notes_risk']
# manual_date = forms.DateField(input_formats=['%d/%m/%Y', '%d-%m-%Y'],
# widget=forms.DateInput(format="%-d/%-m/%Y"))
widgets = {'manual_date': forms.DateInput(attrs={'size':'15','id':'datepicker',
},format='%d/%m/%Y'),'manual_time': forms.TimeInput(attrs={'size':'8','class':'time_field', },format='%H:%M')
}
如何将转换后的日期和时间格式,即日期和时间变量传递给表单。我在表单中指定的格式现在正在显示,但取决于视图的条件,时间和日期格式应该在表单字段中更改。
谢谢