'NoneType'对象没有属性'strftime'

时间:2013-06-07 14:21:34

标签: django django-models django-forms django-templates django-views

views.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})

上面的代码用于转换和显示日期格式。最初,我硬编码数据库中的值并检查格式更改。现在,第一次,对于数据库中的新记录,它给出了此错误“{ {1}}“

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

谢谢

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您必须为report

处理report.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})

答案 2 :(得分: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})