Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django templates echos empty property

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.

like image 264
NemanjaSRB Avatar asked Jan 16 '26 20:01

NemanjaSRB


1 Answers

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
like image 75
relekang Avatar answered Jan 19 '26 18:01

relekang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!