扩大事件发生后使帖子离线的可能性

时间:2019-06-01 09:16:10

标签: python django django-models django-templates

我正在尝试使用以下代码扩展在this question中显示的功能:

@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

我想使用这个新模板:

  {% 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 %}

在Django管理面板内部,我可以正确看到所有内容,如下图所示,但是模板给了我一个空表。如果我删除了返回delta的else条件,则一切正常,那么问题就出在这里,但我不知道为什么如果我使用完整的代码,在Django管理面板中一切都能正常工作。

enter image description here

1 个答案:

答案 0 :(得分:1)

它返回一个空表,因为您的属性is_expired总是返回一个非假值。您的asset.is_expired检查应检查是否asset.is_expired == 'Scaduto'

但是,我建议您在模型中包含以下逻辑:

@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'

然后,您可以根据需要在模板中使用它。如果您想要在管理面板中拥有它,就可以拥有它:

  {% 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 %}