视图.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=datetime.datetime.strptime('10/31/2012','%m/%d/%Y').strftime('%d/%m/%Y')
else:
date=datetime.datetime.strptime('31/10/2012','%d/%m/%Y').strftime('%m/%d/%Y')
if request.method == 'POST':
reportform = ReportForm(instance=report,data=request.POST)
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,
},
context_instance=RequestContext(request))
模型.py
class Report(models.Model):
user = models.ForeignKey(User, null=False)
manual_date = models.DateField('Another date', null=True, blank=True)
模板.html
{{ form.manual_date }}{{ form.manual_date.errors }}{{ date }}
以上视图将模板中的硬编码日期值呈现为变量。可以在模型中显示来自 manual_date 字段的日期,而不是硬编码。以及如何在报表中传递它。