0

I'm trying to expand the function that I've showed in this question using this code:

@property
def is_expired(self):
    print(self.publishing_date)
    if self.membership == "One Month":
        if self.publishing_date + datetime.timedelta(days=30) < datetime.datetime.now():
            message = 'Scaduto'
            return message
        else:
            expiring_date = self.publishing_date + datetime.timedelta(days=30)
            delta = expiring_date - datetime.datetime.now()
            return delta
    else:
        if self.publishing_date + datetime.timedelta(days=365) < datetime.datetime.now():
            message = 'Scaduto'
            return message
        else:
            expiring_date = self.publishing_date + datetime.timedelta(days=365)
            delta = expiring_date - datetime.datetime.now()
            return delta
    return False

I want to use this new template:

  {% for asset in user_details.authorized_user_set.all %}
    {% if not asset.is_expired %}
    <tr>
      <td class="text-left"><p><strong><a class="text-info" href="{{ asset.get_absolute_url }}">{{ asset.title }}</a></strong></p></td>
      <td class="text-center">
        {% if asset.type == "3D Region" %}
          <p class="badge badge-pill badge-primary">{{ asset.type }}</p>
        {% elif asset.type == "3D Village" %}
          <p class="badge badge-pill badge-secondary">{{ asset.type }}</p>
        {% else %}
          <p class="badge badge-pill badge-success">{{ asset.type }}</p>
        {% endif %}
      </td>
      <td class="text-right"><p>{{ asset.publishing_date }}</p></td>
      <td class="text-center">
        {% if asset.membership == "One Month" %}
          <p class="badge badge-pill badge-warning">{{ asset.membership }}</p>
        {% else %}
          <p class="badge badge-pill badge-danger">{{ asset.membership }}</p>
        {% endif %}
      </td>
      <td class="text-right"><p>{{ asset.delta }}</p></td>
    </tr>
    {% endif %}
  {% endfor %}

Inside Django administration panel I can see all that I want corrctly, as you can see on the image below, but the template give me an empty table. If I erase the else condition that return delta everything works fine, then the problem is here but I don't know why everything works fine inside Django administration panel if I use the full code.

enter image description here

MaxDragonheart
  • 1,117
  • 13
  • 34

1 Answers1

1

It returns an empty table because your property is_expired always return a non-false value. Your asset.is_expired check should check if asset.is_expired == 'Scaduto'.

However, I recommend you to have this logic inside your model:

@property
def expiration_date(self):
    if self.membership == "One Month":
        return self.publishing_date + datetime.timedelta(days=30)
    else:
        return self.publishing_date + datetime.timedelta(days=365)

@property
def is_expired(self):
    return self.expiration_date < datetime.datetime.now()

@property
def delta(self):
    return self.expiration_date - datetime.datetime.now() if not self.is_expired else 'Scaduto'

Then, you can use it as you want in the template. If what you want is to have it as you have in the admin panel:

  {% for asset in user_details.authorized_user_set.all %}
    <tr>
      <td class="text-left"><p><strong><a class="text-info" href="{{ asset.get_absolute_url }}">{{ asset.title }}</a></strong></p></td>
      <td class="text-center">
        {% if asset.type == "3D Region" %}
          <p class="badge badge-pill badge-primary">{{ asset.type }}</p>
        {% elif asset.type == "3D Village" %}
          <p class="badge badge-pill badge-secondary">{{ asset.type }}</p>
        {% else %}
          <p class="badge badge-pill badge-success">{{ asset.type }}</p>
        {% endif %}
      </td>
      <td class="text-right"><p>{{ asset.publishing_date }}</p></td>
      <td class="text-center">
        {% if asset.membership == "One Month" %}
          <p class="badge badge-pill badge-warning">{{ asset.membership }}</p>
        {% else %}
          <p class="badge badge-pill badge-danger">{{ asset.membership }}</p>
        {% endif %}
      </td>
      <td class="text-right"><p>{{ asset.delta }}</p></td>
    </tr>
  {% endfor %}
julio
  • 46
  • 4
  • It run, thank you! Have a single simple functions is a better solution because it's possibile to merge every single function for generate a more complex function – MaxDragonheart Jun 01 '19 at 11:18
  • 1
    I can also use `expiration_date` instead of `delta` inside the model and see a countdown with the use of `timeuntil` like this `{{ asset.expiration_date|timeuntil }}` – MaxDragonheart Jun 01 '19 at 11:31