视图.py
def when(request):
user = request.user
try:
report = Report.objects.get(user=user.id)
except:
report = None
settings = Settings.objects.get(user=request.user)
settingsForm = SettingsForm(instance=settings)
if settings.date_format == '0':
date = report.manual_date.strftime('%d/%m/%Y')
else:
date = report.manual_date.strftime('%m/%d/%Y')
reportform = ReportForm(instance=report)
if request.method == 'POST':
reportform = ReportForm(request.POST,instance=report)
if reportform.is_valid():
report = reportform.save(commit=False)
report.user = request.user
report.save()
return redirect('/member/media/')
reportform = ReportForm(instance=report,initial={'manual_date':date})
上面的代码用于转换和显示日期格式。最初,我硬编码数据库中的值并检查格式更改。现在,第一次,对于数据库中的新记录,它给出了这个错误“ 'NoneType' object has no attribute 'strftime'
”
我不知道如何从逻辑上处理这个。需要你的帮助。
谢谢