1

视图.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'

我不知道如何从逻辑上处理这个。需要你的帮助。

谢谢

4

3 回答 3

1

我不知道如何从逻辑上处理这个

manual_date = report.manual_date

if (manual_date):
    format =  (settings.date_format == "0") ? '%d/%m/%Y' : '%m/%d/%Y'
    date = manual_date.strftime(format)

else:
   #No report.manual_date
于 2013-06-07T14:28:37.943 回答
0

您必须同时处理reportreport.manual_dateNone

尝试这个:

def when(request):
    user = request.user
    date = None  
    try:
        report = Report.objects.get(user=user)
    except:
        report = None
    settings = Settings.objects.get(user=user)
    settingsForm = SettingsForm(instance=settings)
    reportform = ReportForm()
    if report and report.manual_date:
        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)
        if report:
            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})
于 2013-06-07T14:25:51.647 回答
0

尝试使用该get_object_or_404()功能:

def when(request):
    user = request.user  
    report = get_object_or_404(Report, user=user) #no need to use the id
    settings = get_object_or_404(Settings, user=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})
于 2013-06-07T14:29:16.253 回答