2016-04-15 79 views
-3

我想做一个项目使用嵌套if。但它不起作用。为什么? 我的代码,嵌套,如果不在django工作

schedule = Schedule.objects.all() 
    for c in schedule : 
     p = c.poll 
     e = c.end_time 
     s = c.start_time 
     n = c.no_of_response 
     now = timezone.now() 
     #phn = Response.objects.filter(poll = p).exclude(sid = 'Null').count() 
     if (c.start_time <= now) & (now <= c.end_time): 
      if n == 0: 
       c.poll.status='Running' 
       c.poll.save() 
+2

是什么不工作?请提供更多信息。 – ilse2005

+0

你没有包含什么'no_of_response'的信息,(我会留下我的答案,因为我仍然认为这是一个错误) – Sayse

+0

if(c.start_time <= now)&(now <= c.end_time ): \t \t \t c.poll.status = '正在运行' \t \t \t c.poll.save()此代码工作。但是,当我把嵌套if,即如果n == 0:,在第一个if条件内,那么整个代码不起作用。为什么会发生? – naveen

回答

2

你正在做与&位比较,你可能需要使用and(或&&

(c.start_time <= now) and (now <= c.end_time) 

或更好,但

c.start_time <= now <= c.end_time 
+0

此代码是正确的。但是不检查if n == 0:。为什么?对不起,因为我的英语不好 – naveen

+0

@naveen - 可能是因为无论出于何种原因,永远不会评估为真,您可能想尝试使用'datetime.now()'代替 – Sayse

+0

此代码正在工作。如果(c.start_time <现在=)(现<= c.end_time): \t \t \t c.poll.status = '正在运行' \t \t \t c.poll.save() – naveen