Django:在活动结束后将帖子置于离线状态 [英] Django: put offline a post after an event

查看:27
本文介绍了Django:在活动结束后将帖子置于离线状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在活动结束后将帖子置于离线状态,确定日期.我开发了一个简单的模型来测试我的目标,并在模型内部放置了一个函数(命名为 is_expired),理想情况下,它必须定义帖子是否在线.下面是模型:

from django.db 导入模型从 django.utils 导入时区导入日期时间类 BlogPost(models.Model):选择 = ((未选择",未选择"),(一个月",一个月"),(一年",一年"),)标题 = 模型.CharField(最大长度=70,唯一=真,)成员资格 = 模型.CharField(最大长度=50,选择=选择,默认=未选择",)发布日期 = 模型.DateTimeField(默认=timezone.now,)def __str__(self):返回 self.title@财产def is_future(self):如果 self.publishing_date >日期时间.日期时间.现在():返回真返回错误@财产def is_expired(self):如果 self.membership == 一个月":每月定义(自己):如果publishing_date + datetime.timedelta(days=30) <= datetime.datetime.now():返回真如果 self.membership == "一年":每年定义(自我):如果publishing_date + datetime.timedelta(days=365) <= datetime.datetime.now():返回真返回错误元类:ordering = ['-publishing_date']

为了显示所有过期帖子的列表,我使用了这个简单的模板:

{% for p in posts_list %}{% 如果 p.is_future 或 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>数据发布:{{ p.publishing_date|date:"d - M - Y | G:i:s" }}</h5>{% 如果 p.is_expired %}<p>过期了?<span class="text-danger"><strong>是</strong></span></p>{% 别的 %}<p>过期了?<span class="text-success"><strong>否</strong></span></p>{% 万一 %}

{% 万一 %}{% 空的 %}<div class="container my-4 bg-primary"><h1>没有帖子!</h1>

{% 结束为 %}

问题是我没有看到过期的帖子,只有未来的帖子(函数名为 is_future),在控制台内没有错误,然后我现在不知道在哪里错误.我是 Python 和 Django 的新手.

有人可以向我指出错误吗?

更新:

views.py

def listPosts(request):posts_list = BlogPost.objects.all()上下文 = {posts_list":posts_list}模板 = '博客/阅读/list_post.html'返回渲染(请求,模板,上下文)

解决方案

你的 is_expired() 方法总是返回 False.在该方法中,您定义了两个内部函数(monthlyyearly),但从不调用它们.那些内部函数其实没用,你要:

@propertydef is_expired(self):如果 self.membership == 一个月":如果 self.publishing_date + datetime.timedelta(days=30) <= datetime.datetime.now():返回真elif self.membership ==一年":如果 self.publishing_date + datetime.timedelta(days=365) <= datetime.datetime.now():返回真返回错误

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)

解决方案

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屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆