django shell条件表达式如果

时间:2016-08-07 11:34:54

标签: django shell

package com.inrvu.adapter;

import java.util.Calendar;
import java.util.Date;


public class Tester {

public static void main(String[] args) {
    Date[] dates = getTwoDatesThreeHoursApart();
    System.out.println(String.format("%b", findIfDatesWithinThreeHours(dates[0],dates[1])));

    dates = getTwoDatesWithinThreeHours();
    System.out.println(String.format("%b", findIfDatesWithinThreeHours(dates[0],dates[1])));
}

public static Date[] getTwoDatesThreeHoursApart()
{
    Calendar calendar = Calendar.getInstance();
    Date date1 = calendar.getTime();
    calendar.add(Calendar.HOUR,4);
    Date date2 = calendar.getTime();
    return new Date[]{date1,date2};
}

public static Date[] getTwoDatesWithinThreeHours()
{
    Calendar calendar = Calendar.getInstance();
    Date date1 = calendar.getTime();
    calendar.add(Calendar.HOUR,3);
    Date date2 = calendar.getTime();
    return new Date[]{date1,date2};
}

public static boolean findIfDatesWithinThreeHours(Date date1, Date date2) {
    // here the time of a single hour is defined as 60*60*1000
    System.out.println(date1);
    System.out.println(date2);
    return Math.abs(date1.getTime()-date2.getTime()) <= 3*60*60*1000;

}
}

这里,一个简单的if else代码在django shell中执行。虽然有一个item.status by value&#34; New&#34 ;,但结果总是false。导致此代码失败的原因是什么?

1 个答案:

答案 0 :(得分:0)

只需检查print(type(item.status)即可返回一个字符串。

确保在与等于运算符进行比较之前对类型数据进行类型转换。