I am creating a django app/website and am in trouble with some Boolean results I don't understand.
In my models, I have a Article class with 2 functions :
class Article(models.Model):
#some vars
basetime = models.IntegerField()
duration = models.IntegerField()
has_begun = models.BooleanField()
def remainingTime(self):
if(self.basetime + self.duration) - time.time() >= 0:
return ((self.basetime + self.duration) - time.time())
else:
return -1
def stillAvailable(self):
if self.remainingTime() >= 0:
return True
return False
And in my views I have a function check :
def check(request,i):
try:
article = Article.objects.get(pk=i)
except Article.DoesNotExist:
return ccm(request)
if (article.stillAvailable):
return test(request,article.remainingTime)
else:
return quid(request)
When a page calls check, my browser displays the test page, and the argument article.remainingTime is -1. (wich is the correct value for what I want to do).
My problem is : if article.remainingTime = -1, then article.stillAvailable should return False, and so the check function should return quid(request). I don't see the reason why django/python interpreter evaluates article.stillAvailable True.
If anyone can help, that'd be very appreciated :P
You are using
if (article.stillAvailable):
As a attribute, rather than calling it as a method. As the attribute exists, it's interpreted as non false. You just need to add the brackets to call the method.