在我的应用程序中,用户可以创建有效期为100天的帖子。现在,我想向用户显示还剩多少天,直到他的帖子被删除。我目前正在解决芹菜工作对工作的实际删除。我的问题只是关于如何显示数字,直到有效性用完为止。 我认为我只需要一个IntegerField,其有效期为几天,然后从创建日期开始减去它。
有一种实用的方法吗?
models.py
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(verbose_name="Post Title", max_length=40)
content = models.TextField(verbose_name="Post Content", max_length=5000)
tag = models.CharField(verbose_name="Tags/Meta - (sep. by comma)", max_length=50, blank=True)
category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
post_type = models.CharField(default='Free', editable=False, max_length=4)
postattachment = fields.FileField(
verbose_name="Post Attachment",
blank=True,
null=True,
upload_to=get_file_path_user_uploads,
validators=[file_extension_postattachment, file_size_postattachment]
)
postcover = fields.ImageField(
null=True,
blank=True,
upload_to=get_file_path_user_uploads,
validators=[default_image_size, default_image_file_extension],
dependencies=[FileDependency(processor=ImageProcessor(
format='PNG', quality=99, scale={'max_width': 700, 'max_height': 700}))])
up_vote = models.IntegerField(verbose_name='Post Up-Vote(s)', default=0)
down_vote = models.IntegerField(verbose_name='Post Down-Vote(s)', default=0)
published_date = models.DateField(auto_now_add=True, null=True)
感谢和亲切问候
更新
models.py
...
down_vote = models.IntegerField(verbose_name='Post Down-Vote(s)', default=0)
published_date = models.DateField(auto_now_add=True, null=True)
def days_remaining(published_date):
"""
Returns the remaining days from 'published_date' + 100 days until today.
"""
return published_date + datetime.timedelta(days=100) - datetime.time.today()
template.html
...
<td>{{ post.days_remaining }}</td>
....
答案 0 :(得分:0)
您可以在模型中定义一个方法来计算,如下所示:
class Post(models.Model):
# ...
published_date = models.DateTimeField(auto_now_add=True, null=True)
def get_time_diff(self):
current_time = datetime.datetime.now()
delta = current_time-self.published_date
return delta.days
在您的代码或模板中使用此方法:
post = Post(...)
if post.get_time_diff > 30:
# do something
更新:如果要计算剩余天数,可以在模型中添加另一个字段(例如days_threshold
),该字段可以保存以下整数:
class Post(models.Model):
# ...
days_threshold = models.IntegerField(default=100)
# ...
在您的get_time_diff
方法中,您可以编写如下内容:
def get_time_diff(self):
current_time = datetime.datetime.now()
delta = current_time-self.published_date
remaining_days = self.days_threshold - delta.days
if remaining_days >= 0
return remaining_days
return 0 # for non negative results in day
或者您可以在方法中添加以此类推的硬编码的days_threshold:
def get_time_diff(self):
current_time = datetime.datetime.now()
delta = current_time-self.published_date
remaining_days = 100 - delta.days # I think this is bad practice but works
if remaining_days >= 0
return remaining_days
return 0 # for non negative results in day
答案 1 :(得分:0)
由于您已经在模型中发布了date_date,因此可以找出发布日期。例如
from datetime import datetime
diff = datetime.now() - published_date
现在,如果max_days = 100(根据您),那么您可以简单地执行max_days-diff.days
将是您的预期结果。
希望对您有帮助。
答案 2 :(得分:0)
您可以执行以下操作:
models.py
from django.db import models
import datetime
# Create your models here.
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(verbose_name="Post Title", max_length=40)
content = models.TextField(verbose_name="Post Content", max_length=5000)
tag = models.CharField(verbose_name="Tags/Meta - (sep. by comma)", max_length=50, blank=True)
category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
post_type = models.CharField(default='Free', editable=False, max_length=4)
postattachment = fields.FileField(
verbose_name="Post Attachment",
blank=True,
null=True,
upload_to=get_file_path_user_uploads,
validators=[file_extension_postattachment, file_size_postattachment]
)
postcover = fields.ImageField(
null=True,
blank=True,
upload_to=get_file_path_user_uploads,
validators=[default_image_size, default_image_file_extension],
dependencies=[FileDependency(processor=ImageProcessor(
format='PNG', quality=99, scale={'max_width': 700, 'max_height': 700}))])
up_vote = models.IntegerField(verbose_name='Post Up-Vote(s)', default=0)
down_vote = models.IntegerField(verbose_name='Post Down-Vote(s)', default=0)
published_date = models.DateField(auto_now_add=True, null=True)
@property
def remaining_days(self):
"""
Returns the remaining days defined as: 'published_date' + 100 days - today.
"""
return (self.published_date + datetime.timedelta(days=100) - datetime.date.today()).days
views.py
from django.views import generic
from .models import Post
class PostDetailView(generic.DetailView):
model = Post
template_name = "post_detail.html"
post_detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Post Detail</title>
</head>
<body>
<p><strong>Days remaining:</strong> {{ post.remaining_days }}</p>
</body>
</html>
屏幕截图: