3

I searched but couldn't find similar question, so I apologies if this was already answered. Now let's get on topic. I have a model with @property values (3 of them). All of them works and one just return empty and I can't find a problem. Here is the part of model:

@property def case_number(self): ''' An formatted number officially designating this case. ''' s = u'%012d' % self.pk return s[:3] + '-' + s[3:6] + '-' + s[6:9] + '-' + s[9:] @property def case_id(self): ''' An formatted string, consisting of the prefix NCDAC- and the case number, officially designating this case. ''' return 'NCDAC-' + self.case_number @property def due_date(self): margin = self.report_date + datetime.timedelta(days=30); if timeuntil(margin) <= datetime.timedelta(days=1): return 'today' elif timeuntil(margin) < 0: return 'overdue' else: return timeuntil(margin).split(', ')[0] 

And here is template part that displays DB results in rows:

 {% for case in sent_cases %} <tr> <td> <a href="{% url "case-detail" case.pk %}"> {{ case.case_number }} </a> </td> <td>{{ case.get_agency_display }}</td> <td>{{ case.report_date }}</td> <td> {% if case.status == "D" %} <i class="icon-minus-sign"></i> No {% else %} <i class="icon-ok-sign"></i> {{ case.ts_submitted|date:"d M Y" }} {% endif %} </td> <td>{{ case.due_date }}</td> </tr> {% endfor %} 

Everything is working except {{ case.due_date }}. It's just empty block. Any idea why this is happening?

Thanks.

UPDATE: Ok I tried just to return "something" and it's working. I have a error bellow - for some reason it doesn't rise Exception though. self.report_date is defined as report_date = models.DateField(verbose_name='Report Date') and I'm trying to get how much days is left untill report_date + 30days.

4
  • 2
    As a test, try calling it from inside your view or from the console instead of the template and see if there's an exception getting thrown. The template renderer eats exceptions. Commented Nov 24, 2012 at 17:30
  • Make sure timeuntil(margin).split(', ')[0] this return some value?? Commented Nov 24, 2012 at 17:40
  • @JoeHolloway I tried calling that prop from View - nothing happened (no Exception). @AamirAdnan I tried previouslly to replace timeuntil(margin).split(', ')[0] with pure string like 'something' and still nothing was echoed in template. Commented Nov 24, 2012 at 17:43
  • 1
    Before returning timeuntil(margin).split(', ')[0] try to print out the return value in func due_date may be your logic is wrong and it is actually returning an empty string. Commented Nov 24, 2012 at 17:45

1 Answer 1

1

The function below should work. I moved the overdue check first, because the today check would also work for those dates since below 0 days also is below 1 day. I also removed the usage of timeuntil since you wrote that you wanted days.

def due_date(self): margin = self.report_date + datetime.timedelta(days=30) time_left = margin - datetime.datetime.now() if time_left < datetime.timedelta(days=0, hours=0, minutes=0): return 'overdue' elif time_left <= datetime.timedelta(days=1): return 'today' else: return '%s days' % time_left.days 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was messing around it but couldn't solve problem with strings and date types. I guess this is a way to learn how carefull I should be when Python and data types are in question :) Thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.