Django:事件发生后将帖子离线 [英] Django: put offline a post after an event

查看:72
本文介绍了Django:事件发生后将帖子离线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将活动(确定日期)后的帖子下线.我已经开发了一个用于测试目标的简单模型,并且在模型内部放置了一个函数(名为is_expired ),理想情况下,该函数必须定义帖子是否在线.下面是模型:

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

问题是我看不到过期的帖子,而只看到将来的帖子(功能为is_future 的将来的帖子),控制台内没有错误,然后我现在不在哪里错误. 我是Python和Django的新手.

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?

更新:

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)

推荐答案

您的is_expired()方法始终返回False.在方法内部,您定义了两个内部函数(monthlyyearly),但是从不调用它们.您想要的那些内部函数实际上是无用的:

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

这篇关于Django:事件发生后将帖子离线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆