Django: put offline a post after an event

2019-08-01 06:15发布

问题:

I'm trying to put offline the posts after an event, a definite date. I've developed a simple model for test my aim and inside the model I've put a function(named is_expired) that, ideally, must define if a post is or not is online. Below there is the model:

from django.db import models
from django.utils import timezone
import datetime

class BlogPost(models.Model):
    CHOICHES = (
        ("Unselected", "Unselected"),
        ("One Month", "One Month"),
        ("One Year", "One Year"),
        )

    title = models.CharField(
        max_length=70,
        unique=True,
        )
    membership = models.CharField(
        max_length=50,
        choices=CHOICHES,
        default="Unselected",
        )
    publishing_date = models.DateTimeField(
                        default=timezone.now,
                        )

    def __str__(self):
        return self.title

    @property
    def is_future(self):
        if self.publishing_date > datetime.datetime.now():
            return True
        return False

    @property
    def is_expired(self):
        if self.membership == "One Month":
            def monthly(self):
                if publishing_date + datetime.timedelta(days=30) <= datetime.datetime.now():
                    return True
        if self.membership == "One Year":
            def yearly(self):
                if publishing_date + datetime.timedelta(days=365) <= datetime.datetime.now():
                    return True
        return False

    class Meta:
        ordering = ['-publishing_date']

For show a list of all expired posts I use this simple template:

{% for p in posts_list %}
  {% if p.is_future or p.is_expired %}
  <div class="container my-4 bg-primary">
    <h3><a class="text-white" href="{{ p.get_absolute_url }}">{{ p.title }}</a></h3>
    <h5>Data di pubblicazione: {{ p.publishing_date|date:"d - M - Y | G:i:s" }}</h5>
    {% if p.is_expired %}
      <p>Expired? <span class="text-danger"><strong>Yes</strong></span></p>
    {% else %}
      <p>Expired? <span class="text-success"><strong>No</strong></span></p>
    {% endif %}
  </div>
  {% endif %}
{% empty %}
  <div class="container my-4 bg-primary">
    <h1>No posts!</h1>
  </div>
{% endfor %}

The problem is that I don't see the expired posts but only the future posts(with function named is_future), inside the console there are no errors and then I don't now where is the error. I'm newbie whit use of Python and Django.

Someone can indicate to me the error?

UPDATE:

views.py

def listPosts(request):
    posts_list = BlogPost.objects.all()
    context = {"posts_list": posts_list}
    template = 'blog/reading/list_post.html'
    return render(request, template, context)

回答1:

Your is_expired() method always returns False. Inside the method, you define two inner function (monthly and yearly) but never call them. Those inner functions are actually useless, you want:

@property
def is_expired(self):
    if self.membership == "One Month":
        if self.publishing_date + datetime.timedelta(days=30) <= datetime.datetime.now():
            return True
    elif self.membership == "One Year":
        if self.publishing_date + datetime.timedelta(days=365) <= datetime.datetime.now():
            return True
    return False